<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/oldimgs/32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/oldimgs/16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=sans-serif:300,300italic,400,400italic,700,700italic|Ubuntu:300,300italic,400,400italic,700,700italic|Roboto:300,300italic,400,400italic,700,700italic|'Open+Sans':300,300italic,400,400italic,700,700italic|'Microsoft+YaHei':300,300italic,400,400italic,700,700italic|sans-serif;:300,300italic,400,400italic,700,700italic|Source+Code+Pro:300,300italic,400,400italic,700,700italic|monospace:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/pace-js@1.0.2/themes/blue/pace-theme-minimal.css">
  <script src="//cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"http:/ShuYuMo2003.github.io","root":"/","scheme":"Mist","version":"8.0.0","exturl":false,"sidebar":{"position":"left","width":230,"display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="济南 清北学堂，NOIP考前十连测，题目整理。">
<meta property="og:type" content="article">
<meta property="og:title" content="「学习总结」清北学堂 十连测">
<meta property="og:url" content="2020/%E3%80%8C%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93%E3%80%8D%E6%B8%85%E5%8C%97%E5%AD%A6%E5%A0%82-%E5%8D%81%E8%BF%9E%E6%B5%8B/index.html">
<meta property="og:site_name" content="Shu Yu Mo&#39;s blog">
<meta property="og:description" content="济南 清北学堂，NOIP考前十连测，题目整理。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="/post_images/qbxt_Round0_C.png">
<meta property="article:published_time" content="2020-12-01T01:16:52.000Z">
<meta property="article:modified_time" content="2021-01-09T13:13:51.117Z">
<meta property="article:author" content="舒雨墨">
<meta property="article:tag" content="学习总结">
<meta property="article:tag" content="好题">
<meta property="article:tag" content="题解">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/post_images/qbxt_Round0_C.png">


<link rel="canonical" href="2020/「学习总结」清北学堂-十连测/">


<script data-pjax class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>「学习总结」清北学堂 十连测 | Shu Yu Mo's blog</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Shu Yu Mo's blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">远行者</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/link/" rel="section"><i class="fa fa-th fa-fw"></i>links</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-templates">

    <a href="/port/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>Templates</a>

  </li>
        <li class="menu-item menu-item-latex">

    <a href="/LaTeX-syntax.html" rel="section"><i class="fa fa-heartbeat fa-fw"></i>LaTeX</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav" style="display:none;">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">

        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="舒雨墨"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">舒雨墨</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">106</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">28</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">53</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/ShuYuMo2003" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShuYuMo2003" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.luogu.com.cn/user/44615" title="Luogu → https:&#x2F;&#x2F;www.luogu.com.cn&#x2F;user&#x2F;44615" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Luogu</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:sujiayi2003@gmail.com" title="E-Mail → mailto:sujiayi2003@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://twitter.com/JiaYiSu5" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;JiaYiSu5" rel="noopener" target="_blank"><i class="fab fa-twitter fa-fw"></i>Twitter</a>
      </span>
  </div>



        <hr/>
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B8%85%E5%8C%97%E5%AD%A6%E5%A0%82-10-%E8%BF%9E%E6%B5%8B"><span class="nav-number">1.</span> <span class="nav-text">清北学堂 10 连测</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-1"><span class="nav-number">1.1.</span> <span class="nav-text">Round 1</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-yist"><span class="nav-number">1.1.1.</span> <span class="nav-text">A. yist</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-ernd"><span class="nav-number">1.1.2.</span> <span class="nav-text">B. ernd</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-sanrd"><span class="nav-number">1.1.3.</span> <span class="nav-text">C. sanrd</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%87%A0%E4%B8%AA%E4%BA%8B%E5%AE%9E"><span class="nav-number">1.1.3.1.</span> <span class="nav-text">几个事实</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-sith"><span class="nav-number">1.1.4.</span> <span class="nav-text">D. sith</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-2"><span class="nav-number">1.2.</span> <span class="nav-text">Round 2</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-one"><span class="nav-number">1.2.1.</span> <span class="nav-text">A. one</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-two"><span class="nav-number">1.2.2.</span> <span class="nav-text">B. two</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-three"><span class="nav-number">1.2.3.</span> <span class="nav-text">C. three</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9A%E4%B9%89%E4%B8%80%E7%B1%BB%E7%89%B9%E6%AE%8A%E6%A0%87%E8%AE%B0"><span class="nav-number">1.2.3.1.</span> <span class="nav-text">定义一类特殊标记</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E8%BF%90%E7%AE%97%E6%8E%A7%E5%88%B6%E7%B2%BE%E5%BA%A6"><span class="nav-number">1.2.3.2.</span> <span class="nav-text">浮点数运算控制精度</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-four"><span class="nav-number">1.2.4.</span> <span class="nav-text">D. four</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-3"><span class="nav-number">1.3.</span> <span class="nav-text">Round 3</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-diyiti"><span class="nav-number">1.3.1.</span> <span class="nav-text">A. diyiti</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-dierti"><span class="nav-number">1.3.2.</span> <span class="nav-text">B. dierti</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-disanti"><span class="nav-number">1.3.3.</span> <span class="nav-text">C. disanti</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#statement"><span class="nav-number">1.3.3.1.</span> <span class="nav-text">statement</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#input"><span class="nav-number">1.3.3.2.</span> <span class="nav-text">input</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-disiti"><span class="nav-number">1.3.4.</span> <span class="nav-text">D. disiti</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-4"><span class="nav-number">1.4.</span> <span class="nav-text">Round 4</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E4%B8%80"><span class="nav-number">1.4.1.</span> <span class="nav-text">A. 一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-%E4%BA%8C"><span class="nav-number">1.4.2.</span> <span class="nav-text">B. 二</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E4%B8%89"><span class="nav-number">1.4.3.</span> <span class="nav-text">C. 三</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E5%9B%9B"><span class="nav-number">1.4.4.</span> <span class="nav-text">D. 四</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-5"><span class="nav-number">1.5.</span> <span class="nav-text">Round 5</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E4%B8%80-1"><span class="nav-number">1.5.1.</span> <span class="nav-text">A. 一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-%E4%BA%8C-1"><span class="nav-number">1.5.2.</span> <span class="nav-text">B. 二</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E4%B8%89-1"><span class="nav-number">1.5.3.</span> <span class="nav-text">C. 三</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E5%9B%9B-1"><span class="nav-number">1.5.4.</span> <span class="nav-text">D. 四</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-6"><span class="nav-number">1.6.</span> <span class="nav-text">Round 6</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E4%B8%80-2"><span class="nav-number">1.6.1.</span> <span class="nav-text">A. 一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-%E4%BA%8C-2"><span class="nav-number">1.6.2.</span> <span class="nav-text">B. 二</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E4%B8%89-2"><span class="nav-number">1.6.3.</span> <span class="nav-text">C. 三</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E5%9B%9B-2"><span class="nav-number">1.6.4.</span> <span class="nav-text">D. 四</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-7"><span class="nav-number">1.7.</span> <span class="nav-text">Round 7</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E4%B8%80-3"><span class="nav-number">1.7.1.</span> <span class="nav-text">A. 一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-%E4%BA%8C-3"><span class="nav-number">1.7.2.</span> <span class="nav-text">B. 二</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E4%B8%89-3"><span class="nav-number">1.7.3.</span> <span class="nav-text">C. 三</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E5%9B%9B-3"><span class="nav-number">1.7.4.</span> <span class="nav-text">D. 四</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-8"><span class="nav-number">1.8.</span> <span class="nav-text">Round 8</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E5%8F%8C%E6%8C%82"><span class="nav-number">1.8.1.</span> <span class="nav-text">A. 双挂</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-DDL%E9%80%89%E6%89%8B"><span class="nav-number">1.8.2.</span> <span class="nav-text">B. DDL选手</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E7%BA%A6%E6%95%B0%E9%93%BE"><span class="nav-number">1.8.3.</span> <span class="nav-text">C. 约数链</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E8%BF%9E%E9%80%9A"><span class="nav-number">1.8.4.</span> <span class="nav-text">D. 连通</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Round-9"><span class="nav-number">1.9.</span> <span class="nav-text">Round 9</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#A-%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0"><span class="nav-number">1.9.1.</span> <span class="nav-text">A. 最大公约数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-%E7%B4%A7%E6%80%A5%E5%87%BA%E5%8F%A3"><span class="nav-number">1.9.2.</span> <span class="nav-text">B. 紧急出口</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%80%8D%E5%A2%9EDP"><span class="nav-number">1.9.2.1.</span> <span class="nav-text">倍增DP</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9A%94%E6%9D%BF%E6%B3%95"><span class="nav-number">1.9.2.2.</span> <span class="nav-text">隔板法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-%E5%90%88%E5%BD%B1"><span class="nav-number">1.9.3.</span> <span class="nav-text">C. 合影</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#D-%E8%B7%AF%E5%BE%84"><span class="nav-number">1.9.4.</span> <span class="nav-text">D. 路径</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%81%9A%E6%B3%95%E4%B8%80%EF%BC%9A%E6%A0%91%E5%89%96-std-set-%E7%BB%B4%E6%8A%A4%E6%A0%87%E8%AE%B0-%E6%A0%87%E8%AE%B0%E6%B0%B8%E4%B9%85%E5%8C%96%E7%BA%BF%E6%AE%B5%E6%A0%91"><span class="nav-number">1.9.4.1.</span> <span class="nav-text">做法一：树剖 +  std::set 维护标记 标记永久化线段树</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%81%9A%E6%B3%95%E4%BA%8C%EF%BC%9A%E7%BA%BF%E6%AE%B5%E6%A0%91%E7%BB%B4%E6%8A%A4%E8%B7%AF%E5%BE%84%E4%BA%A4%E9%9B%86-%E4%BA%8C%E5%88%86%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="nav-number">1.9.4.2.</span> <span class="nav-text">做法二：线段树维护路径交集 + 二分最小值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%81%9A%E6%B3%95%E4%B8%89%EF%BC%9ACDQ-%E5%88%86%E6%B2%BB"><span class="nav-number">1.9.4.3.</span> <span class="nav-text">做法三：CDQ 分治</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="舒雨墨"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">舒雨墨</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">106</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">28</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">53</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/ShuYuMo2003" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShuYuMo2003" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.luogu.com.cn/user/44615" title="Luogu → https:&#x2F;&#x2F;www.luogu.com.cn&#x2F;user&#x2F;44615" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Luogu</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:sujiayi2003@gmail.com" title="E-Mail → mailto:sujiayi2003@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://twitter.com/JiaYiSu5" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;JiaYiSu5" rel="noopener" target="_blank"><i class="fab fa-twitter fa-fw"></i>Twitter</a>
      </span>
  </div>



      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="/2020/「学习总结」清北学堂-十连测/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="舒雨墨">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Shu Yu Mo's blog">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          「学习总结」清北学堂 十连测
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-12-01 09:16:52" itemprop="dateCreated datePublished" datetime="2020-12-01T09:16:52+08:00">2020-12-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-01-09 21:13:51" itemprop="dateModified" datetime="2021-01-09T21:13:51+08:00">2021-01-09</time>
      </span>

  
    <span id="2020/「学习总结」清北学堂-十连测/" class="post-meta-item leancloud_visitors" data-flag-title="「学习总结」清北学堂 十连测" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/「学习总结」清北学堂-十连测/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="2020/「学习总结」清北学堂-十连测/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
      </div>
      <div class="post-meta">
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>55k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>50 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>济南 清北学堂，NOIP考前十连测，题目整理。</p>
<a id="more"></a>
<h1 id="清北学堂-10-连测"><a href="#清北学堂-10-连测" class="headerlink" title="清北学堂 10 连测"></a>清北学堂 10 连测</h1><h2 id="Round-1"><a href="#Round-1" class="headerlink" title="Round 1"></a>Round 1</h2><h3 id="A-yist"><a href="#A-yist" class="headerlink" title="A. yist"></a>A. yist</h3><div class="note info"><p>给出 $n$ 求 $n! \operatorname{mod} 2^{32}$。</p>
</div>

<details class="note warning"><summary><p>Code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	A<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>A<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> i<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token keyword">int</span> T <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>T<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">&lt;=</span> <span class="token number">50</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%llu\n"</span><span class="token punctuation">,</span> A<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="B-ernd"><a href="#B-ernd" class="headerlink" title="B. ernd"></a>B. ernd</h3><div class="note info"><p>有一个 $n\times m$ 的网格，网格上的数字都在 $[1,nm]$ 之间且两两不同。</p>
<p>有个限制，直观描述是位置左、上的格子必须比右、下的格子小，精确地说：$A_{i,j} &lt; A_{i+1,j},A_{i,j} &lt; A_{i,j+1}$ 必须成立，如果不等式中的两个位置都在网格内的话。</p>
<p>你想知道对于一个 $k$，有多少个格子的值可以是 $k$（即存在一种网格，使得它的值是 $k$）。</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>考虑每个位置 $(i, j)$ 能填哪些数字，这个位置到 $(1, 1)$ 形成的矩形中的值都要大于 $(i, j)$ 的值，这个位置到 $n, m$ 同理。</p>
<details class="note warning"><summary><p>Code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> ans<span class="token punctuation">[</span>_T<span class="token punctuation">]</span><span class="token punctuation">,</span> C<span class="token punctuation">[</span>_T<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span>  m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>C<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>C<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> L <span class="token operator">=</span> i <span class="token operator">*</span> j<span class="token punctuation">,</span> R <span class="token operator">=</span> n <span class="token operator">*</span> m <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>n <span class="token operator">-</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>m <span class="token operator">-</span> j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		C<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span> C<span class="token punctuation">[</span>R <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> now <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">*</span> m<span class="token punctuation">)</span> now <span class="token operator">+=</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> now<span class="token punctuation">;</span>
	<span class="token keyword">int</span> q <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>q<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> ans<span class="token punctuation">[</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
</details>

<h3 id="C-sanrd"><a href="#C-sanrd" class="headerlink" title="C. sanrd"></a>C. sanrd</h3><div class="note info"><p>小Y有一个排列 $P_{1..n}$，作为强迫症患者，她想要把排列排好序。共有 3 种操作。</p>
<p>花费 $a$ 的代价交换相邻两个数。</p>
<p>花费 $b$ 的代价翻转整个排列。</p>
<p>花费 $c$ 的代价打乱整个排列，新排列随机生成。</p>
<p>小Y需要知道，在最优策略下，她需要花费多大的代价呢？</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>最优的操作，显然是先打乱排列（如果有），再翻转排列（如果有），最后交换相邻两个数（如果有）。</p>
<div class="note danger"><h4 id="几个事实"><a href="#几个事实" class="headerlink" title="几个事实"></a>几个事实</h4><ul>
<li>$A_i &gt; A_j, i + 1 = j$ 是交换相邻两个数字的必要条件。</li>
<li>执行一次交换操作逆序对数量会减少且仅减少一。</li>
<li>答案只与逆序对数量有关。</li>
<li>打乱后随机生成的代价就是 $c$ + 长度为 $n$ 的所有排列排好代价的平均值。只要使用最后一种操作，那么他们的答案一定相同。</li>
</ul>
</div>
<p>设排列 $[P]$ 的逆序对数为 $x$ ，如果只使用前两种操作，<br>代价就是 $\operatorname{min}{\left(\frac{n(n - 1)}{2} - x\right) \times a + b, x \times a}$。<br>把 $[p]$ 的所有排列按照只用前两种操作的花费排好序，那么可能使用最后一种操作的一定是一段后缀。</p>
<p>以 $[p]$ 的所有排列按照只用前两种操作的花费排好序的下标为 $x$ 轴，代价为 $y$ 轴，那么只用前两种操作就是绿色的图像。 如果对某些排列使用第三种操作，可以用橙色的图像表示。<br><img src="/post_images/qbxt_Round0_C.png" alt="Round0_C.png"><br>其中被函数值压平的部分就是 $c$ + 所有排列代价的平均值。<br>枚举是多长的后缀被压平（使用第三种操作）。然后解方程解出函数值平均值，选取最小的平均值即可。</p>
<p>如果排列数比较多，无法一个一个的算出来，可以通过 dp 求出长度为 $n$ 的排列，逆序对个数为 $k$ 的排列数。可以注意到，逆序对数相同的排列，答案一定是一样的，可以把逆序对是相同的排列捆绑计算，逆序对数一共有 $\frac{n \times (n - 1)}{2} + 1$ 种取值。</p>
<details class="note warning"><summary><p>我的代码只能通过 70% 的数据</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstdio></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstring></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cmath></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cmath></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;algorithm></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;climits></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;assert.h></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;vector></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">LL <span class="token keyword">long</span> <span class="token keyword">long</span> </span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token keyword">int</span> <span class="token keyword">long</span> <span class="token keyword">long</span> </span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">,</span> ___ <span class="token operator">=</span> <span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> ___<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">per</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">,</span> ___ <span class="token operator">=</span> <span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> ___<span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">Read_t</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">></span> <span class="token keyword">const</span> Read_t <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span> T <span class="token operator">&amp;</span> target <span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">&#123;</span>
		T x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span> sign <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>c <span class="token operator">&lt;</span> <span class="token string">'0'</span> <span class="token operator">||</span> c <span class="token operator">></span> <span class="token string">'9'</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>c <span class="token operator">==</span> <span class="token string">'-'</span><span class="token punctuation">)</span> sign <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>c <span class="token operator">&lt;=</span> <span class="token string">'9'</span> <span class="token operator">&amp;&amp;</span> c <span class="token operator">>=</span> <span class="token string">'0'</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> <span class="token punctuation">(</span>x <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>x <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> x <span class="token operator">+=</span> c <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		target <span class="token operator">=</span> x <span class="token operator">*</span> sign<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">*</span><span class="token keyword">this</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> 
<span class="token punctuation">&#125;</span> Read<span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">warning</span> <span class="token expression">dont forget enable int_64</span></span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _N <span class="token operator">=</span> <span class="token number">500</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _S <span class="token operator">=</span> <span class="token number">500</span><span class="token punctuation">;</span>
LL <span class="token function">gcd</span><span class="token punctuation">(</span>LL a<span class="token punctuation">,</span> LL b<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> b <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> a <span class="token operator">:</span> <span class="token function">gcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span>  a <span class="token operator">%</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
LL dp<span class="token punctuation">[</span>_N<span class="token punctuation">]</span><span class="token punctuation">[</span>_S<span class="token punctuation">]</span><span class="token punctuation">;</span>
LL n<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> d<span class="token punctuation">;</span> 
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">pb push_back</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">mp make_pair</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">fi first</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">se second</span></span>
pair<span class="token operator">&lt;</span>LL<span class="token punctuation">,</span> LL<span class="token operator">></span> Val<span class="token punctuation">[</span>_S<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator">></span> <span class="token punctuation">(</span>pair<span class="token operator">&lt;</span>LL<span class="token punctuation">,</span> LL<span class="token operator">></span> A<span class="token punctuation">,</span> pair<span class="token operator">&lt;</span>LL<span class="token punctuation">,</span> LL<span class="token operator">></span> B<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>A<span class="token punctuation">.</span>fi <span class="token operator">*</span> B<span class="token punctuation">.</span>se <span class="token operator">></span> A<span class="token punctuation">.</span>se <span class="token operator">*</span> B<span class="token punctuation">.</span>fi<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
LL A<span class="token punctuation">[</span>_S<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">doit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span> vector<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span>LL<span class="token punctuation">,</span> LL<span class="token operator">></span> <span class="token operator">></span> V<span class="token punctuation">;</span> V<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> V<span class="token punctuation">.</span><span class="token function">pb</span><span class="token punctuation">(</span><span class="token function">mp</span><span class="token punctuation">(</span><span class="token function">min</span><span class="token punctuation">(</span>i <span class="token operator">*</span> a<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> i<span class="token punctuation">)</span> <span class="token operator">*</span> a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">,</span> dp<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>V<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> V<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> V<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> R <span class="token operator">=</span> L<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>V<span class="token punctuation">[</span>R <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">==</span> V<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">&amp;&amp;</span> R <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>V<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> R<span class="token operator">++</span><span class="token punctuation">;</span> LL sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span> sum <span class="token operator">+=</span> V<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>

		V<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">=</span> V<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">.</span>fi<span class="token punctuation">;</span> V<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>se <span class="token operator">=</span> sum<span class="token punctuation">;</span>
		tot<span class="token operator">++</span><span class="token punctuation">;</span>
		L <span class="token operator">=</span> R<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> LL S <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> tot <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> S <span class="token operator">+=</span> V<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> tot <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		LL cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> i<span class="token punctuation">,</span> tot <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> cnt <span class="token operator">+=</span> V<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>
		LL A <span class="token operator">=</span> cnt <span class="token operator">*</span> c<span class="token punctuation">;</span>  <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> A <span class="token operator">+=</span> V<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">*</span> V<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>
		LL B <span class="token operator">=</span> S <span class="token operator">-</span> cnt<span class="token punctuation">;</span>
		LL g <span class="token operator">=</span> <span class="token function">gcd</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span> Val<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span>A <span class="token operator">/</span> g<span class="token punctuation">,</span> B <span class="token operator">/</span> g<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	
	<span class="token keyword">int</span> Mid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> tot <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>Val<span class="token punctuation">[</span>Mid<span class="token punctuation">]</span> <span class="token operator">></span> Val<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> Mid <span class="token operator">=</span> i<span class="token punctuation">;</span>
	pair<span class="token operator">&lt;</span>LL<span class="token punctuation">,</span> LL<span class="token operator">></span> MAns <span class="token operator">=</span> Val<span class="token punctuation">[</span>Mid<span class="token punctuation">]</span><span class="token punctuation">;</span> MAns<span class="token punctuation">.</span>fi <span class="token operator">+=</span> MAns<span class="token punctuation">.</span>se <span class="token operator">*</span> c<span class="token punctuation">;</span> LL g <span class="token operator">=</span> <span class="token function">gcd</span><span class="token punctuation">(</span>MAns<span class="token punctuation">.</span>fi<span class="token punctuation">,</span> MAns<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span> MAns<span class="token punctuation">.</span>fi <span class="token operator">/=</span> g<span class="token punctuation">;</span> MAns<span class="token punctuation">.</span>se <span class="token operator">/=</span> g<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>d<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		LL ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ans <span class="token operator">+=</span> <span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">></span> A<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans <span class="token operator">*</span> a<span class="token punctuation">,</span> b <span class="token operator">+</span> a <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> ans<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ans <span class="token operator">*</span> MAns<span class="token punctuation">.</span>se <span class="token operator">&lt;</span> MAns<span class="token punctuation">.</span>fi<span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld/1\n"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld/%lld\n"</span><span class="token punctuation">,</span> MAns<span class="token punctuation">.</span>fi<span class="token punctuation">,</span> MAns<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">assert</span><span class="token punctuation">(</span>MAns<span class="token punctuation">.</span>fi <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token function">assert</span><span class="token punctuation">(</span>MAns<span class="token punctuation">.</span>se <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">Init</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			LL <span class="token operator">&amp;</span>ans <span class="token operator">=</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token function">min</span><span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span> ans <span class="token operator">+=</span> dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> k<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">signed</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token function">freopen</span><span class="token punctuation">(</span><span class="token string">"in.txt"</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">Init</span><span class="token punctuation">(</span><span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> T<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>T<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>T<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token function">doit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<details class="note warning"><summary><p>官方标程</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">fi first</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">se second</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">pb push_back</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">mp make_pair</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">SZ</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>x<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ALL</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> x<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> x<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">L</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> u<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> i<span class="token punctuation">;</span> i <span class="token operator">=</span> nxt<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">per</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> ld<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ui<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span> ll<span class="token operator">></span> Pll<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Vi<span class="token punctuation">;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">read</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> f <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isdigit</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c <span class="token operator">==</span> <span class="token string">'-'</span><span class="token punctuation">)</span> f <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">isdigit</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> c <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> x <span class="token operator">*=</span> f<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
T <span class="token function">gcd</span><span class="token punctuation">(</span>T a<span class="token punctuation">,</span> T b<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token operator">!</span>b <span class="token operator">?</span> a <span class="token operator">:</span> <span class="token function">gcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> a <span class="token operator">%</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umin</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">&lt;</span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umax</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">></span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
ll dp<span class="token punctuation">[</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">16</span> <span class="token operator">|</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">141</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">Yzr</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">;</span>
	ll gs<span class="token punctuation">[</span><span class="token number">141</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">ini</span><span class="token punctuation">(</span><span class="token keyword">int</span> nn<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		n <span class="token operator">=</span> nn<span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> n<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">~</span>s <span class="token operator">>></span> i <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> del <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> del <span class="token operator">+=</span> s <span class="token operator">>></span> j <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> cnt <span class="token operator">+=</span> s <span class="token operator">>></span> j <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> cnt <span class="token operator">*</span> <span class="token punctuation">(</span>cnt <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>s <span class="token operator">|</span> <span class="token number">1</span> <span class="token operator">&lt;&lt;</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">+</span> del<span class="token punctuation">]</span> <span class="token operator">+=</span> dp<span class="token punctuation">[</span>s<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> gs<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> n<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> yzr<span class="token punctuation">[</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">cmp</span><span class="token punctuation">(</span>Pll a<span class="token punctuation">,</span> Pll b<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>ld<span class="token punctuation">)</span>a<span class="token punctuation">.</span>fi <span class="token operator">/</span> a<span class="token punctuation">.</span>se <span class="token operator">&lt;</span> <span class="token punctuation">(</span>ld<span class="token punctuation">)</span>b<span class="token punctuation">.</span>fi <span class="token operator">/</span> b<span class="token punctuation">.</span>se<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
ll n<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> d<span class="token punctuation">,</span> qz<span class="token punctuation">[</span><span class="token number">141</span><span class="token punctuation">]</span><span class="token punctuation">,</span> p<span class="token punctuation">[</span><span class="token number">166</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
Pll s<span class="token punctuation">[</span><span class="token number">166</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
ll <span class="token function">calc</span><span class="token punctuation">(</span>ll x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token function">min</span><span class="token punctuation">(</span>x <span class="token operator">*</span> a<span class="token punctuation">,</span> <span class="token punctuation">(</span>n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span> <span class="token operator">-</span> x<span class="token punctuation">)</span> <span class="token operator">*</span> a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">freopen</span><span class="token punctuation">(</span><span class="token string">"in.txt"</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span> yzr<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">ini</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> T<span class="token punctuation">;</span>
	<span class="token function">read</span><span class="token punctuation">(</span>T<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span> <span class="token punctuation">(</span>T<span class="token operator">--</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
		Pll <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">1e18</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		ll tot <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> tot <span class="token operator">*=</span> i<span class="token punctuation">;</span>
		<span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>yzr<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">.</span>gs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> s<span class="token punctuation">[</span><span class="token operator">++</span>len<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span><span class="token function">calc</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span> yzr<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">.</span>gs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">sort</span><span class="token punctuation">(</span>s <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> s <span class="token operator">+</span> len <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">static</span> ll qz<span class="token punctuation">[</span><span class="token number">166</span><span class="token punctuation">]</span><span class="token punctuation">,</span> hz<span class="token punctuation">[</span><span class="token number">166</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> len<span class="token punctuation">)</span> qz<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> qz<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">*</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>
		hz<span class="token punctuation">[</span>len <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">per</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> len<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> hz<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> hz<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se<span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> len<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			Pll cur <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span>qz<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> hz<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> c<span class="token punctuation">,</span> tot <span class="token operator">-</span> hz<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">cmp</span><span class="token punctuation">(</span>cur<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">)</span>
				res <span class="token operator">=</span> cur<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span> ll<span class="token operator">></span> ans <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>fi <span class="token operator">+</span> c <span class="token operator">*</span> res<span class="token punctuation">.</span>se<span class="token punctuation">,</span> res<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span>
		cerr <span class="token operator">&lt;&lt;</span> <span class="token string">"N = "</span> <span class="token operator">&lt;&lt;</span> n <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		cout <span class="token operator">&lt;&lt;</span> ans<span class="token punctuation">.</span>fi <span class="token operator">&lt;&lt;</span> <span class="token string">" "</span> <span class="token operator">&lt;&lt;</span> ans<span class="token punctuation">.</span>se <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span>d<span class="token operator">--</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">read</span><span class="token punctuation">(</span>p<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> nx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> nx <span class="token operator">+=</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">></span> p<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
			Pll ans <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span><span class="token function">calc</span><span class="token punctuation">(</span>nx<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">cmp</span><span class="token punctuation">(</span><span class="token function">mp</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>fi <span class="token operator">+</span> c <span class="token operator">*</span> res<span class="token punctuation">.</span>se<span class="token punctuation">,</span> res<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">)</span>
				ans <span class="token operator">=</span> <span class="token function">mp</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>fi <span class="token operator">+</span> c <span class="token operator">*</span> res<span class="token punctuation">.</span>se<span class="token punctuation">,</span> res<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span>
			ll g <span class="token operator">=</span> <span class="token function">gcd</span><span class="token punctuation">(</span>ans<span class="token punctuation">.</span>fi<span class="token punctuation">,</span> ans<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span>
			ans<span class="token punctuation">.</span>fi <span class="token operator">/=</span> g<span class="token punctuation">;</span>
			ans<span class="token punctuation">.</span>se <span class="token operator">/=</span> g<span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld/%lld\n"</span><span class="token punctuation">,</span> ans<span class="token punctuation">.</span>fi<span class="token punctuation">,</span> ans<span class="token punctuation">.</span>se<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
</details>
<h3 id="D-sith"><a href="#D-sith" class="headerlink" title="D. sith"></a>D. sith</h3><div class="note info"><p>你有 $k$ 棵点数均为 $n$ 的树</p>
<p>对于每对点 $i,j$，你都需要求出，有多少个点 $x$，满足在所有树中都在 $i\leftrightarrow j$ 的树链上（树链包含端点即 $i,j$）。</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>定义函数 $\operatorname{dis_t}(x, y)$ 为 $x$，$y$ 这两个结点在树 $t$ 上的最短距离（简单路径长度）。</p>
<p>对于一棵树 $t$ ，“$\operatorname{dis_t}(i, j) = \operatorname{dis_t}(i, x) + \operatorname{dis_t}(x, j)$” 为 “点 $x$ 在 点$i$ $\leftrightarrow$ 点 $j$ 的树链上” 的充要条件。</p>
<p>扩展一下可以发现</p>
<p>对于森林 $T$，“$\sum_{t \in T}\limits{\operatorname{dis_t}(i, j)} = \sum_{t \in T}\limits{\operatorname{dis_t}(i, x)} + \sum_{t \in T}\limits{\operatorname{dis_t}(x, j)}$” 为 “对于每棵树 点 $x$ 在 点$i$ $\leftrightarrow$ 点 $j$ 的树链上” 的充要条件。</p>
<p>这样就可以分开算了，随便做就好了。</p>
<details class="note warning"><summary><p>code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">fi first</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">se second</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">pb push_back</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">mp make_pair</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">SZ</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>x<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ALL</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> x<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> x<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">L</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> u<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> i<span class="token punctuation">;</span> i <span class="token operator">=</span> nxt<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">per</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> ld<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ui<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> Pii<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Vi<span class="token punctuation">;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">read</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> f <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isdigit</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>c <span class="token operator">==</span> <span class="token string">'-'</span><span class="token punctuation">)</span>f <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">isdigit</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>x <span class="token operator">=</span> x <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> c <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span>c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span>x <span class="token operator">*=</span> f<span class="token punctuation">;</span><span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
T <span class="token function">gcd</span><span class="token punctuation">(</span>T a<span class="token punctuation">,</span> T b<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token operator">!</span>b <span class="token operator">?</span> a <span class="token operator">:</span> <span class="token function">gcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> a <span class="token operator">%</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umin</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">&lt;</span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umax</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">></span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> T <span class="token function">Abs</span><span class="token punctuation">(</span><span class="token keyword">const</span> T <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> x <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">?</span> x <span class="token operator">:</span> <span class="token operator">-</span>x<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> ui <span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">static</span> ui seed <span class="token operator">=</span> <span class="token number">613</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> seed <span class="token operator">^=</span> seed <span class="token operator">>></span> <span class="token number">5</span><span class="token punctuation">,</span> seed <span class="token operator">^=</span> seed <span class="token operator">&lt;&lt;</span> <span class="token number">17</span><span class="token punctuation">,</span> seed <span class="token operator">^=</span> seed <span class="token operator">>></span> <span class="token number">13</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">505</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> dis<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span>
Vi e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> fa<span class="token punctuation">,</span> <span class="token keyword">int</span> dep<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	dis<span class="token punctuation">[</span>s<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">+=</span> dep<span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> v <span class="token operator">:</span> e<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>v <span class="token operator">!=</span> fa<span class="token punctuation">)</span>
			<span class="token function">dfs</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> v<span class="token punctuation">,</span> u<span class="token punctuation">,</span> dep <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">read</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>tt<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span><span class="token function">read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">read</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
			e<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">pb</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>e<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">pb</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">dfs</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ans <span class="token operator">+=</span> dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> dis<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">==</span> dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d%c"</span><span class="token punctuation">,</span> ans<span class="token punctuation">,</span> j <span class="token operator">&lt;</span> n <span class="token operator">?</span> <span class="token string">' '</span> <span class="token operator">:</span> <span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
</details>


<h2 id="Round-2"><a href="#Round-2" class="headerlink" title="Round 2"></a>Round 2</h2><h3 id="A-one"><a href="#A-one" class="headerlink" title="A. one"></a>A. one</h3><div class="note info"><p>给你一个长度为 $n$ 的数组，你需要将其划分为若干个连续段。对于一种划分，定义其权值为，求出每段的段内所有元素 xor 值，再把所有段的 xor 值相加即为权值。</p>
<p>你需要计算对于所有划分，这个权值的最小、最大值分别是多少。<br>$n \le 10^6$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>$A \operatorname{xor} B \le A + B$</p>
<p>$A \ \And B \le A\ |\ B \le A + B$</p>
<p>全部拆开最大，全部合并最小。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span> MIN <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> LL MAX <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> x<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> MIN <span class="token operator">^=</span> x<span class="token punctuation">;</span> MAX <span class="token operator">+=</span> x<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d %lld"</span><span class="token punctuation">,</span> MIN<span class="token punctuation">,</span> MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="B-two"><a href="#B-two" class="headerlink" title="B. two"></a>B. two</h3><div class="note info"><p>有一个 $1..n$ 依次连成的环，有一个从 $1$ 开始移动的指针，每次指针所在位置有$p$的概率消失并将这个位置对应的下标（在 $1..n$ 中）插入序列 $B$ 的末尾，然后指针移动一格（ $1$ 移到 $2$，$n$ 移到 $1$ 这样，一个位置若已经消失则不会被移动到）。所有位置都消失时游戏结束。最后 $B$ 会是一个排列。</p>
<p>这道题跟序列 $B$ 没什么关系，你只需要求出游戏期望进行几轮，答案对 $998244353$ 取模。</p>
<p>读入描述：<br>一行三个整数 $n,x,y$。概率 $p=\frac x y$</p>
</div>
<details class="note success"><summary><p>solution</p>
</summary>
<p>设 $f[n]$ 为 长度为 $n$ 还需要进行多少轮。</p>
<p>易知 $f[n] = 1 + p \times f[n - 1] + \left(1 - p \right) \times f[n]$</p>
<p>移项得 $f[n] = n \times \frac{1}{p}$</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">(</span>n <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> y <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token function">inv</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="C-three"><a href="#C-three" class="headerlink" title="C. three"></a>C. three</h3><div class="note info"><p>小Y拥有一个序列$a_i$（从0开始标号）。</p>
<p>小Y想要对序列进行$Q$次操作，操作有下面几种：</p>
<ul>
<li>$t=0$：对区间$[l,r]$执行$a_i=a_i+x$</li>
<li>$t=1$：对区间$[l,r]$执行$a_i=\lfloor \frac {a_i} {x} \rfloor$</li>
<li>$t=2$：查询区间$[l,r]$的$\max {a_i}$</li>
<li>$t=3$：把区间$[l,r]$的$a_i$恢复为初始给出的$a_i$<br>$a_i \le 10^8; n, Q \le 10^5$</li>
</ul>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<h4 id="定义一类特殊标记"><a href="#定义一类特殊标记" class="headerlink" title="定义一类特殊标记"></a>定义一类特殊标记</h4><p>记录标记 $a, b, c$ 表示 $x$ 变成 $\lfloor \frac{x + a}{b} \rfloor + c$。 其中要求 $a &lt; b$。</p>
<ul>
<li>对于加法操作 $c += x$ 即可。</li>
<li>对于除法操作如果是除 $d$ ：$\lfloor \frac{ \lfloor \frac{x + a}{b} \rfloor + c}{d} \rfloor = \lfloor \frac{ \lfloor \frac{x + a + bc}{b} \rfloor}{d} \rfloor = \lfloor \frac{x + a + bc}{bd} \rfloor$ 然后优化一下 $x + a + bc$ 保持 $a’ &lt; b’$，类似于假分数换算带分数，保证不会爆<code>long long</code>。</li>
</ul>
<p>对于处理除数过大的情况：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">if</span> <span class="token punctuation">(</span>fm <span class="token operator">></span> inf<span class="token punctuation">)</span> fz <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token number">0ll</span><span class="token punctuation">,</span> fz <span class="token operator">+</span> <span class="token punctuation">(</span>inf <span class="token operator">-</span> fm<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> fm <span class="token operator">=</span> inf<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>如果分母 $&gt; \operatorname{inf}$，那么 $\lfloor \frac{x + a}{b} \rfloor + c$ 等价于 $[x \ge b - a] + c$<br>当分母很大时，可以使得 $a, b$ 同时减去一个常数使得 $b$ 不会溢出，对于 $[x \ge b - a] + c$ 不影响取值。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">fi first</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">se second</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">pb push_back</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">mp make_pair</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">SZ</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>x<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ALL</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> x<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> x<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">L</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> u<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> i<span class="token punctuation">;</span> i <span class="token operator">=</span> nxt<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">per</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">register</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> ld<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ui<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> Pii<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Vi<span class="token punctuation">;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
T <span class="token function">gcd</span><span class="token punctuation">(</span>T a<span class="token punctuation">,</span> T b<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token operator">!</span>b <span class="token operator">?</span> a <span class="token operator">:</span> <span class="token function">gcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> a <span class="token operator">%</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umin</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">&lt;</span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">></span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">umax</span><span class="token punctuation">(</span>T <span class="token operator">&amp;</span>x<span class="token punctuation">,</span> T y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> x <span class="token operator">=</span> x <span class="token operator">></span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> ui <span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">static</span> ui seed <span class="token operator">=</span> <span class="token number">416</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> seed <span class="token operator">^=</span> seed <span class="token operator">>></span> <span class="token number">5</span><span class="token punctuation">,</span> seed <span class="token operator">^=</span> seed <span class="token operator">&lt;&lt;</span> <span class="token number">17</span><span class="token punctuation">,</span> seed <span class="token operator">^=</span> seed <span class="token operator">>></span> <span class="token number">13</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">233333</span><span class="token punctuation">,</span> inf <span class="token operator">=</span> <span class="token number">2e9</span> <span class="token operator">+</span> <span class="token number">1e8</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">node</span>
<span class="token punctuation">&#123;</span>
	ll ans<span class="token punctuation">;</span>
	<span class="token keyword">bool</span> emp<span class="token punctuation">;</span>
	ll a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> mx<span class="token punctuation">;</span> <span class="token comment">//(x+a)/b+c (a&lt;b&lt;=inf)</span>
	<span class="token function">node</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> b <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> tree<span class="token punctuation">[</span>N <span class="token operator">&lt;&lt;</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushadd</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> ll x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">+=</span> x<span class="token punctuation">;</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c <span class="token operator">+=</span> x<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushdiv</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> d<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">/=</span> d<span class="token punctuation">;</span>
	ll fm <span class="token operator">=</span> <span class="token number">1ll</span> <span class="token operator">*</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">*</span> d<span class="token punctuation">,</span> tmp <span class="token operator">=</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token operator">+</span> <span class="token number">1ll</span> <span class="token operator">*</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">*</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">)</span> <span class="token operator">/</span> fm<span class="token punctuation">;</span>
	ll fz <span class="token operator">=</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token operator">+</span> <span class="token number">1ll</span> <span class="token operator">*</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">*</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">)</span> <span class="token operator">-</span> fm <span class="token operator">*</span> tmp<span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c <span class="token operator">=</span> tmp<span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>fm <span class="token operator">></span> inf<span class="token punctuation">)</span> fz <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token number">0ll</span><span class="token punctuation">,</span> fz <span class="token operator">+</span> <span class="token punctuation">(</span>inf <span class="token operator">-</span> fm<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> fm <span class="token operator">=</span> inf<span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token operator">=</span> fz<span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">=</span> fm<span class="token punctuation">;</span>
	<span class="token comment">//	printf("%d:%lld %lld %lld\n",k,tree[k].a,tree[k].b,tree[k].c);</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushemp</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>emp <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token operator">=</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">=</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>mx<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushdown</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>emp<span class="token punctuation">)</span> <span class="token function">pushemp</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">pushemp</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>emp <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span> <span class="token function">pushadd</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">pushadd</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">pushdiv</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">pushdiv</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">)</span> <span class="token function">pushadd</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">pushadd</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">)</span><span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>c <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">upd</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>ans<span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>ans<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">mdy1</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">==</span> L <span class="token operator">&amp;&amp;</span> r <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">pushadd</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">mdy1</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">mdy1</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token function">mdy1</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">mdy1</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">upd</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">mdy2</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">==</span> L <span class="token operator">&amp;&amp;</span> r <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">pushdiv</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">mdy2</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">mdy2</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token function">mdy2</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">mdy2</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">upd</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">mdy3</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">==</span> L <span class="token operator">&amp;&amp;</span> r <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">pushemp</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">mdy3</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">mdy3</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token function">mdy3</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">mdy3</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">upd</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">qry</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">==</span> L <span class="token operator">&amp;&amp;</span> r <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans<span class="token punctuation">;</span>
	<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token function">qry</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">></span> mid<span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token function">qry</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token function">qry</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> x<span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">qry</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> q<span class="token punctuation">,</span> a<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">==</span> r<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>mx <span class="token operator">=</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">=</span> a<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>l <span class="token operator">+</span> r<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">build</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">build</span><span class="token punctuation">(</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>mx <span class="token operator">=</span> tree<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mx<span class="token punctuation">,</span> tree<span class="token punctuation">[</span>k <span class="token operator">&lt;&lt;</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mx<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">read</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">build</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span> <span class="token punctuation">(</span>q<span class="token operator">--</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> op<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">;</span>
		<span class="token function">read</span><span class="token punctuation">(</span>op<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">read</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>op <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">mdy1</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>op <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">mdy2</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>op <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> <span class="token function">qry</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token function">mdy3</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">,</span> x<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="浮点数运算控制精度"><a href="#浮点数运算控制精度" class="headerlink" title="浮点数运算控制精度"></a>浮点数运算控制精度</h4><p>直接全部转化成浮点数运算，三标记线段树。</p>
<p>考虑到浮点数自带精度损失，对于较大的除数，自动使得原数变成零，<code>long double</code> 的精度可以支持。</p>
<div class="note warning"><p>爆零小技巧：<code>ST</code>表空间开一倍</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> ST<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>LOG <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> Log<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">init_query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> LOG<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ST<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token punctuation">(</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ST[i + (1 &lt;&lt; (j - 1))][j - 1] 会访问 O( 2 * N )</span>
	Log<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> Log<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> Log<span class="token punctuation">[</span>i <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> q<span class="token punctuation">;</span> LL A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">namespace</span> Acceptable_Solution<span class="token punctuation">&#123;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> LOG <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ST<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>LOG <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> Log<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">init_query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> LOG<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ST<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token punctuation">(</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		Log<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> Log<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> Log<span class="token punctuation">[</span>i <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">Query_Max</span><span class="token punctuation">(</span><span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> Lg <span class="token operator">=</span> Log<span class="token punctuation">[</span>R <span class="token operator">-</span> L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> Len <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> Lg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">max</span><span class="token punctuation">(</span>ST<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">[</span>Lg<span class="token punctuation">]</span><span class="token punctuation">,</span> ST<span class="token punctuation">[</span>R <span class="token operator">-</span> Len <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>Lg<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">LB <span class="token keyword">long</span> <span class="token keyword">double</span> </span></span>
	LB eps<span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span>LB x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%.20Lf\n"</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">namespace</span> SegmentTree<span class="token punctuation">&#123;</span>
		<span class="token keyword">bool</span> <span class="token function">same</span><span class="token punctuation">(</span>LB x<span class="token punctuation">,</span> LB y<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token function">fabs</span><span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">)</span> <span class="token operator">&lt;=</span> <span class="token number">1e-12</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">6e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> ch<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		LB v<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> tag_mul<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">bool</span> tag_ret<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">make</span> <span class="token punctuation">(</span>tot<span class="token operator">++</span><span class="token punctuation">,</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tag_mul<span class="token punctuation">[</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> tag_ret<span class="token punctuation">[</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tot<span class="token punctuation">)</span></span></span>
		<span class="token keyword">int</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> make<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> A<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span> <span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span>
			<span class="token function">build</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">tar_ret</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">Query_Max</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
			tag_ret<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">push_ret</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>tag_ret<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">tar_ret</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">tar_ret</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			tag_ret<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">tar_add</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> LB Val<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token function">push_ret</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span> 
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">+=</span> Val<span class="token punctuation">;</span>
			tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">+=</span> Val<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">tar_mul</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> LB Val<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			<span class="token function">push_ret</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span> 
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">*=</span> Val<span class="token punctuation">;</span> 
			tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">*=</span> Val<span class="token punctuation">;</span>
			tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">*=</span> Val<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token function">push_ret</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">same</span><span class="token punctuation">(</span>tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
				<span class="token function">tar_mul</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token function">tar_mul</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
				tag_mul<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">same</span><span class="token punctuation">(</span>tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token function">tar_add</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token function">tar_add</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
				tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> <span class="token keyword">int</span> Val<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">tar_add</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> Val<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">update_mul</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> LB Val<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">tar_mul</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> Val<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update_mul</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update_mul</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">void</span> <span class="token function">update_ret</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">tar_ret</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update_ret</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update_ret</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		LB <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> nowr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			LB ans <span class="token operator">=</span> LLONG_MIN<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> 
	<span class="token punctuation">&#125;</span><span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>build<span class="token punctuation">;</span>       <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>query<span class="token punctuation">;</span>       <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update_add<span class="token punctuation">;</span>
	 <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update_mul<span class="token punctuation">;</span>  <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update_ret<span class="token punctuation">;</span>  <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>Groot<span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">init_query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> root <span class="token operator">=</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span> LB one <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>q<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> t<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> x<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">(</span>L<span class="token punctuation">)</span><span class="token punctuation">(</span>R<span class="token punctuation">)</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> L <span class="token operator">++</span><span class="token punctuation">,</span> R <span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">update_mul</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> one <span class="token operator">/</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld\n"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">long</span> <span class="token keyword">long</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token function">query</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1e-10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token function">update_ret</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">//freopen("in.txt", "r", stdin);</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token class-name">Acceptable_Solution</span><span class="token operator">::</span><span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="D-four"><a href="#D-four" class="headerlink" title="D. four"></a>D. four</h3><div class="note info"><p>你有 $1..2n$ 共 $2n$ 个数，有 $n$ 个人，你会给每个人分两个随机的数，这样就把 $2n$ 个数分完了，每个人获得的总数就是这两个数相加。这个随机过程可以认为是，先把 $2n$ 个数随机排列，将前2个数分给第一个人，将接下来前2个数分给第二个人……不难看出这个过程是没有歧义的</p>
<p>你想知道有多大概率，使得总和最大的人唯一（不存在两个相同的最大值），答案模 $998244353$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>没听懂…。</p>

</details>

<h2 id="Round-3"><a href="#Round-3" class="headerlink" title="Round 3"></a>Round 3</h2><h3 id="A-diyiti"><a href="#A-diyiti" class="headerlink" title="A. diyiti"></a>A. diyiti</h3><div class="note info"><p>有 $n$ 个物品，每个物品价格 $A_i$ ，收益为 $B_i$ ，由于一些奥妙重重的原因，当你买了一些物品，总价格为选择的 $A_i$ 的按位或而不是代数相加!</p>
<p>你身上只有 $k$ 元钱，你希望选择总价格不超过 $k$ 的一些物品，来获得最大的收益。</p>
<p>对于所有数据，$1\leq n\leq 10^5,0\leq k &lt; 2^{30},0 \leq A_i &lt; 2^{30},0 \leq B_i \leq 10^9$。</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>一种天然的想法就是考虑枚举，每一个 $i \le k$ 然后累加所有可以被选的物品求最大值，显然能保证正确性。得到一个 $\mathcal{O}(k n)$ 的优秀做法。<br>发现这样枚举的每一个 $i$ 有很多没有必要的枚举。</p>
<p>其实可以枚举放弃 $k$ 的哪一位 <code>1</code>。</p>
<p>例如: 若 $k = (1001101001)_2$ 那么有意义的 $i$ 就可能是：</p>
<ul>
<li>$(0111111111)_2$</li>
<li>$(1000111111)_2$</li>
<li>$(1001011111)_2$</li>
<li>$(1001100111)_2$</li>
<li>$(1001101001)_2$</li>
</ul>
<p>也就是枚举某一位 $1$ 把这个 $1$ 变成 $0$，然后把这后面的所有数字变成 $1$。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">;</span>
<span class="token keyword">int</span> Lim<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> Pr<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
LL <span class="token function">calc</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	LL ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x <span class="token operator">|</span> Lim<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> x<span class="token punctuation">)</span> ans <span class="token operator">+=</span> Pr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>Lim<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span>Pr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	LL ans <span class="token operator">=</span> <span class="token function">calc</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">30</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>k <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> Lit <span class="token operator">=</span> k<span class="token punctuation">;</span>
		Lit <span class="token operator">^=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> Lit <span class="token operator">|=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> i<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token function">calc</span><span class="token punctuation">(</span>Lit<span class="token punctuation">)</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="B-dierti"><a href="#B-dierti" class="headerlink" title="B. dierti"></a>B. dierti</h3><div class="note info"><p>给你一个长度为 $n$ 的数组 $a_n$，你需要求出一个最长的子序列 $C$，使得其相邻两项按位与的结果均非零 $( \forall\ i,\ C_i\ \And \  C_{i+1} &gt; 0) $。</p>
</div>

<details class="note success"><summary><p>Solution</p>
</summary>
<p>这比第一题好想多了吧。<br>考虑 $dp[n]$ 表示 $[1, n]$ 中强制 $n$ 一定选 符合要求的最长子序列，可以考虑枚举 $[1..n - 1]$ 中上一个选择的是哪一个，即<br> $dp[n] = 1 + \max_{i = 1}^{n - 1}\limits{dp[i][C_i \And C_n &gt; 0]}$<br>这样的复杂度是 $\mathcal{O}(n^2)$ 的。</p>
<p>考虑如果满足 $C_i \And C_j &gt; 0$ 那么需要 $C_i, C_j$ 至少一个二进制位置相同。那就维护一个数组，$Bmax[i]$ 表示 $C$ 的第 $i$ 位为 $1$ 的所有 $C_j$ 最大的 $dp[j]$，即可实现 $\mathcal{O}(\log C_{max})$ 转移。同时需要 $\mathcal{O}(\log C_{max})$ 维护数组 $Bmax$。</p>
<p>总复杂度 $\mathcal{O}(n\log C_{max} )$。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> Bmax<span class="token punctuation">[</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	n <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span> Bmax<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>Bmax<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> Bmax<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> ans <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span> Bmax<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>Bmax<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span> cerr <span class="token operator">&lt;&lt;</span> <span class="token string">"std's ans = "</span> <span class="token operator">&lt;&lt;</span> ans <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="C-disanti"><a href="#C-disanti" class="headerlink" title="C. disanti"></a>C. disanti</h3><div class="note info"><h4 id="statement"><a href="#statement" class="headerlink" title="statement"></a>statement</h4><p>你有 $n$ 个 $10$ 进制下的位数为 $m$ 的数</p>
<p>有些数字被抹去了，因此取值任意。</p>
<p>你希望这 $n$ 个数单调递增，请求出所有满足这个条件的方案下，这 $n$ 个数之和的总和。</p>
<p>答案对 $998244353$ 取模。</p>
<p>$n,m\leq 30$</p>
<h4 id="input"><a href="#input" class="headerlink" title="input"></a>input</h4><p>第一行两个整数 $n,m(1\leq n,m\leq 50)$</p>
<p>接下来 $n$ 行，每行 $m$ 个字符，字符为 $0..9$ 的数字或者<code>?</code>。</p>
<pre class="line-numbers language-none"><code class="language-none">INPUT0:
4 1
0
?
4
8
OUTPUT0:
42<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-none"><code class="language-none">INPUT1:
5 5
?1234
23333
???66
??666
?233?
OUTPUT1:
819525655<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="note danger"><p>没听懂… 待补。</p>
</div>

<h3 id="D-disiti"><a href="#D-disiti" class="headerlink" title="D. disiti"></a>D. disiti</h3><div class="note info"><p>你有一堆$n$个石子的石子堆，你想要把石子堆裂成$n$个大小为$1$的石子堆。为了达成这个目的，你每次可以进行如下操作：假设现在有$k$堆石子，大小分别为$a_1, a_2, … ,a_k$，你可以指定一个非负整数序列$b_1, b_2, … ,b_k$，满足$\sum b_i\leq m$，每堆石子就会分裂成两堆$b_i, a_i-b_i$（如果为$0$就认为不存在，显然还需要满足$b_i \leq a_i$）。你想要知道最少几次操作可以达成目的。<br>对于所有数据，$T\leq 1000,1\leq m\leq n\leq 10^9$</p>
</div>
<div class="note danger"><p>没听懂… 待补。</p>
</div>

<h2 id="Round-4"><a href="#Round-4" class="headerlink" title="Round 4"></a>Round 4</h2><h3 id="A-一"><a href="#A-一" class="headerlink" title="A. 一"></a>A. 一</h3><div class="note info"><div class="note danger"><p>你是能看到第一题的friends呢。 ——hja<br>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。</p>
</div>

<p>现在我们要执行若干代码，代码为对于变量的操作，包含以下三种：</p>
<ul>
<li>1、变量名=变量值，变量名由小写字母组成，变量值有可能是正整数或者字符串（小写字母或者数字），例如：a=3,b=”233”。</li>
<li>2、变量名，代表询问该变量的值，如果该变量未赋值，则输出no。</li>
<li>3、变量名+=值，如果该变量未定义，直接跳过该次操作。设该变量的值为x，加上的值为y，如果两者均为整数直接相加；如果x是整数y为字符串则跳过此次操作；如果x是字符串y是整数则将y转换为字符串进行字符串拼接；如果两者均为字符串直接进行字符串拼接。</li>
<li></div>
<details class="note success"><summary><p>code</p>
</summary>
<p>全程 STL。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">map<span class="token operator">&lt;</span>string <span class="token punctuation">,</span> string<span class="token operator">></span> M<span class="token punctuation">;</span>
map<span class="token operator">&lt;</span>string <span class="token punctuation">,</span> <span class="token keyword">bool</span> <span class="token operator">></span> Type<span class="token punctuation">;</span> <span class="token comment">// true for string , false for int</span>
string tmp<span class="token punctuation">;</span>
pair<span class="token operator">&lt;</span>string <span class="token punctuation">,</span> string<span class="token operator">></span> Res<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">split</span><span class="token punctuation">(</span>string S<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">>=</span> S<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">||</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">// TODO: check</span>
		<span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'+'</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> S<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'+'</span><span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Res <span class="token operator">=</span> <span class="token function">make_pair</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'+'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
			Res <span class="token operator">=</span> <span class="token function">make_pair</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span>S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> S<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> S<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
string <span class="token function">calc</span><span class="token punctuation">(</span>string A<span class="token punctuation">,</span> string B<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">long</span> <span class="token keyword">long</span> ResA <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		ResA <span class="token operator">*=</span> <span class="token number">10</span><span class="token punctuation">;</span>
		ResA <span class="token operator">+=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">long</span> <span class="token keyword">long</span> ResB <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		ResB <span class="token operator">*=</span> <span class="token number">10</span><span class="token punctuation">;</span>
		ResB <span class="token operator">+=</span> B<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ResA <span class="token operator">+=</span> ResB<span class="token punctuation">;</span>
	string Res<span class="token punctuation">,</span> ans <span class="token punctuation">;</span> Res <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>ResA<span class="token punctuation">)</span> Res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>ResA <span class="token operator">%</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> ResA <span class="token operator">/=</span> <span class="token number">10</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> Res<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span> ans<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>Res<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">bool</span> <span class="token function">pdType</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>S<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> S<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'"'</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token comment">// true for string</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">//freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);</span>
	ios<span class="token operator">::</span><span class="token function">sync_with_stdio</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> q<span class="token punctuation">;</span> cin <span class="token operator">>></span> q<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>q<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		cin <span class="token operator">>></span> tmp<span class="token punctuation">;</span>
		<span class="token keyword">int</span> r <span class="token operator">=</span> <span class="token function">split</span><span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>r <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> re <span class="token operator">=</span> <span class="token function">pdType</span><span class="token punctuation">(</span>Res<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>re<span class="token punctuation">)</span> Res<span class="token punctuation">.</span>second <span class="token operator">=</span> <span class="token punctuation">(</span>Res<span class="token punctuation">.</span>second<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token operator">?</span> <span class="token function">string</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span> <span class="token operator">:</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">=</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
			Type<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">=</span> re<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>r <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>M<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">)</span>  cout <span class="token operator">&lt;&lt;</span> M<span class="token punctuation">[</span>tmp<span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
			<span class="token keyword">else</span> cout <span class="token operator">&lt;&lt;</span> <span class="token string">"no"</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>M<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> ex_t <span class="token operator">=</span> <span class="token function">pdType</span><span class="token punctuation">(</span>Res<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex_t<span class="token punctuation">)</span> Res<span class="token punctuation">.</span>second <span class="token operator">=</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> no_t <span class="token operator">=</span> Type<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span> ex_t <span class="token operator">&amp;&amp;</span>  no_t<span class="token punctuation">)</span> M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">=</span> M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">+</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span> ex_t <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>no_t<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>ex_t <span class="token operator">&amp;&amp;</span>  no_t<span class="token punctuation">)</span> M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">=</span> M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">+</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>ex_t <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>no_t<span class="token punctuation">)</span> M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">calc</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>Res<span class="token punctuation">.</span>first<span class="token punctuation">]</span><span class="token punctuation">,</span> Res<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="B-二"><a href="#B-二" class="headerlink" title="B. 二"></a>B. 二</h3><div class="note info"><p>你是能看到第二题的friends呢。 ——aoao</p>
<p>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。<br>$N$ 个小写字母字符串， Alice 和 Bob 按照如下方法玩游戏：</p>
<ul>
<li>1、Alice选择一个字符串，并重新定义 $26$ 个字母的字典序，然后再将这个字符串内部字符按照任意顺序重排。</li>
<li>2、Bob从剩下的字符串中选择一个，并将这个字符串内部字符按照任意顺序重排。<br>他们的目标是使得自己字符串的字典序比对面小，谁的字符串字典序更小谁就赢了。问 Alice 有多少个可以选择的字符串能够使得自己获胜？<br>$N \le 1000$</li>
</ul>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>因为可以重新排列字符串，所以对于每个字符串来说，有用的信息就是每个字符串中每个字符的出现次数。</p>
<p>可以考虑先枚举一个字符串，然后判断这个字符串是否能被 Alice 选。然后考虑确定字典序，可以以此考虑字典序最小的应该是哪个字符，确定了字典序最小的字符之后，可以排除掉一些字符串（字典序最小的字符在这些串中的出现次数 $&lt;$ 在枚举串中的出现次数），然后又转化成原来相同的问题。</p>
<p>关键在于如何确定当前情况下字典序最小的字符。显然，理想的字典序最小的字符 在当前枚举到的字符串中的出现次数一定不少于在其他串中的出现次数。但是这样的字符可能有多个，应该选哪个？</p>
<p>其实选哪个效果一样，本质上只是以此选择当前情况下字典序最小的字符，然后剔除一些字符串，但是剔除字符串的条件和剔除顺序无关。</p>
<p>最后判断是否所有字符串都被剔除掉即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstdio></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstring></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cmath></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstring></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;algorithm></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">,</span> __ <span class="token operator">=</span> <span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> __<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">1010</span><span class="token punctuation">;</span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">int</span> <span class="token function">idx</span><span class="token punctuation">(</span><span class="token keyword">char</span> c<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> c <span class="token operator">-</span> <span class="token string">'a'</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> ch<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> n<span class="token punctuation">;</span> 
<span class="token keyword">char</span> S<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> book<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> bch<span class="token punctuation">[</span><span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%s"</span><span class="token punctuation">,</span> S <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token function">strlen</span><span class="token punctuation">(</span>S <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span> ch<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token function">idx</span><span class="token punctuation">(</span>S<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">++</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>book<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>book<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">memset</span><span class="token punctuation">(</span>bch<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>bch<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'z'</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>bch<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> 
				<span class="token keyword">bool</span> pass <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
				<span class="token function">rep</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
					<span class="token keyword">if</span><span class="token punctuation">(</span>book<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
					<span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token function">idx</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">></span> ch<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token function">idx</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> pass <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
				<span class="token punctuation">&#125;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>pass<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					bch<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					<span class="token function">rep</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token function">idx</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> ch<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token function">idx</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span> book<span class="token punctuation">[</span>l<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					<span class="token keyword">break</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">bool</span> pass <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> pass <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		ans <span class="token operator">+=</span> pass<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

</li>
</ul>
<h3 id="C-三"><a href="#C-三" class="headerlink" title="C. 三"></a>C. 三</h3><div class="note info"><p>你是能看到第三题的friends呢。 ——laekov<br>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。</p>
<p>给定$N$个数，设总共有$M$个区间$p_1=[l_1,r_1],p_2=[l_2,r_2 ],\cdots,p_M=[l_m,r_m]$的逆序对数量不少于$K$个。定义函数$f(p_i,p_j)$为计算两个区间的交集大小的函数（即共同覆盖了多少个数）。求<br>$\sum_{i=1}^M\sum_{j=i+1}^M f(p_i,p_j)$<br>$N \le 10^6$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>考虑每一个元素的贡献，不难发现，元素的贡献为 $\binom{被覆盖的区间数}{2}$ ，问题转化为怎么求每个元素被覆盖的次数。</p>
<p>考虑对于每一个左端点 $L$，合法的右端点取值一定是一段连续的区间，且 如果存在上界一定是 $n$ ，对于每个左端点，求出其对应的最小的右端点，这里可以使用 <code>two-point</code> 。值域树状数组统计逆序对数。</p>
<p>考虑每个左端点为 $L$ 的区间对元素覆盖数的贡献，发现一定是一条平直线和一个下降直线，可以对差分数组的差分数组做修改，然后两次前缀和还原出原序列。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> inv2 <span class="token operator">=</span> <span class="token number">500000004</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">namespace</span> BIT<span class="token punctuation">&#123;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">lowbit</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">(</span>x <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token operator">-</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
	<span class="token keyword">int</span> C<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> p<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>p <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> p<span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token function">lowbit</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+=</span> x<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span>  <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> p<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> p<span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">-=</span> <span class="token function">lowbit</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span> ans <span class="token operator">+=</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ans<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">using</span> BIT<span class="token operator">::</span>add<span class="token punctuation">;</span> <span class="token keyword">using</span> BIT<span class="token operator">::</span>query<span class="token punctuation">;</span>
<span class="token keyword">int</span> L <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> R <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> Rp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> C<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	LL ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">add</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> ans <span class="token operator">-=</span> <span class="token function">query</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>R <span class="token operator">&lt;</span> L<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>ans <span class="token operator">&lt;</span> k <span class="token operator">&amp;&amp;</span> R <span class="token operator">&lt;=</span> n<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>R <span class="token operator">==</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> R<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span><span class="token operator">++</span>R<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ans <span class="token operator">+=</span> <span class="token function">query</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">query</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>R<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		Rp<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">=</span> R<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>Rp<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">></span> n<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">int</span> cnt <span class="token operator">=</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> Rp<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		C<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> cnt<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span> C<span class="token punctuation">[</span>L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>MOD <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> cnt<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		C<span class="token punctuation">[</span>Rp<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>Rp<span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> MOD <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> C<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> C<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token punctuation">(</span>C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> inv2 <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="D-四"><a href="#D-四" class="headerlink" title="D. 四"></a>D. 四</h3><div class="note info"><p>你是能看到第四题的 friends 呢。 ——laekov</p>
<p>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。</p>
<p>今天也是皮克敏们打工的一天，你有 $N$ 只皮克敏，每只皮克敏有一颗炸弹。现在有一颗 $N$ 个点的树，同时皮克敏们可以从 $p_1$ 或者 $p_2$ 两个点中的任意一个进入树。每次你需要派一只皮克敏去到某个点，然后将炸弹连同皮克敏和这个点一起炸掉。一个点被炸掉了之后皮克敏就再也不能通过了。现在你需要决定皮克敏们炸点的顺序，问 $n!$ 种炸点方案中有多少种能炸掉所有点。<br>$N \le 1000$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>考虑 <code>DP</code> 。</p>
<p>首先考虑一个子树（子树内没有 $p_1, p_2$）全部被清空应该是什么顺序，一定是每个儿子的子树先被清空然后清楚当前点。 这里可以使用树形 dp 处理。</p>
<p>先考虑 $p_1 \leftrightarrow p_2$ 这一条链上的点应该是按照什么顺序被清空，任意时刻，被清空的点一定是链上的连续的一段，这里可以区间 $dp$ 统计答案，转移考虑最后一次删除是删干净了链上最右边的点还是最左边的点。</p>
<p>然后和 $p_1, p_2$ 的每个不在链上的儿子合并答案，最后决策先删除 $p_1$ 还是 $p_2$。<br>代码还没写。</p>

</details>

<h2 id="Round-5"><a href="#Round-5" class="headerlink" title="Round 5"></a>Round 5</h2><h3 id="A-一-1"><a href="#A-一-1" class="headerlink" title="A. 一"></a>A. 一</h3><div class="note info"><p>现在有四种颜色的东西，各有$n_1,n_2,n_3,n_4$个。你需要把他们放到一排里面，并且保证相邻的东西颜色不同，问方案数。<br>对于$80%$的数据，$n_1+n_2+n_3+n_4\leq 10$。<br>对于另外$10%$的数据，$n_1=0,n_2,n_3,n_4\leq 50$。<br>对于$100%$的数据，$0\leq n_1,n_2\leq 200,0\leq n_3,n_4\leq50000$。</p>
</div>
<details class="note success"><summary><p>code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">namespace</span> subtask1<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> dp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> A<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> B<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> C<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> D<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">+</span> j <span class="token operator">+</span> k <span class="token operator">+</span> l <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span>  dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>j <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span>  dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>k <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span>  dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>l <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span>  dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> ans1 <span class="token operator">=</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1ll</span><span class="token operator">*</span> dp<span class="token punctuation">[</span>A<span class="token punctuation">]</span><span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span>  dp<span class="token punctuation">[</span>A<span class="token punctuation">]</span><span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span>  dp<span class="token punctuation">[</span>A<span class="token punctuation">]</span><span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span>  dp<span class="token punctuation">[</span>A<span class="token punctuation">]</span><span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">namespace</span> subtask2<span class="token punctuation">&#123;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">53</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> dp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> B<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> C<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> D<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">+</span> j <span class="token operator">+</span> k <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>j <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>k <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> ans2 <span class="token operator">=</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token punctuation">(</span> dp<span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">[</span>C<span class="token punctuation">]</span><span class="token punctuation">[</span>D<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="B-二-1"><a href="#B-二-1" class="headerlink" title="B. 二"></a>B. 二</h3><div class="note info"><p>$N$个二元组$(a_i,b_i)$，定义$c_1=a_1+b_1,c_i=b_i+max⁡(c_{i-1},\sum_{j=1}^ia_j)$。现在你可以随意重排这$N$个二元组，求$c_N$的最小值<br>$N \le 10^6; A, B \le N$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>考虑相邻两个二元组 $(a_1, b_1) (a_2, b_2)$ 应该怎么比较大小。<br>分别列出 $(a_1, b_1)$ 在 $(a_2, b_2)$ 之前的答案和交换之后的答案。</p>
<p>如果 $(a_1, b_1)$ 在 $(a_2, b_2)$ 之前：设这两个二元组之前所有二元组中 $a$ 的和为 $x$，设上一个二元组的 $C$ 值为 $y$。<br>根据定义 $C_1 = b_1 + \max(y, a_1 + x)$，$C_2 = b_2 + max(C_1, x + a_1 + a_2)$<br>化简后得到<br>$C_2 = \max(y + b_1 + b_2, x + a_1 + b_1 + b_2, x + a_1 + a_2 + b_2)$<br>易知 交换后<br>$C_2’ = \max(y + b_1 + b_2, x + a_2 + b_1 + b_2, x + a_1 + a_2 + b_1)$<br>现在需要比较 $C_2$ 和 $C_2’$ 的大小。相当于对 $6$ 个式子取最大值，看最大值出现在哪边。相同的项 $y + b_1 + b_2$ 可以消去，如果其为最大值，那么两个式子谁在前谁在后无所谓，如果其不是最大值，那么也没有影响。 消去每一项中相同的 $x$。</p>
<p>$C_2 = \max(a_1 + b_1 + b_2, a_1 + a_2 + b_2)$</p>
<p>$C_2’ = \max(a_2 + b_1 + b_2, a_1 + a_2 + b_1)$</p>
<p>提出共同的项：<br>$C_2 = a_1 + b_2 + \max(b_1,a_2)$</p>
<p>$C_2’ = a_2 + b_1 + \max(b_2, a_1)$</p>
<p>假设 $C_2 &lt; C_2’$</p>
<p>则 $a_1 + b_2 + \max(b_1,a_2) &lt; a_2 + b_1 + \max(b_2, a_1)$</p>
<p>移项得 $a_1 + b_2 - \max(b_2,a_1) &lt; a_2 + b_1 - \max(b_1, a_2)$</p>
<p>得出 $\min(a_1, b_2) &lt; \min(a_2, b_1)$</p>
<p>一种特殊情况是取等的时候：<del>举几个栗子得出</del> 结论是取等时比较 $a_1 &lt; a_2$。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">bool</span> <span class="token function">CMP</span><span class="token punctuation">(</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> x<span class="token punctuation">,</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> y<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> r0 <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span>first<span class="token punctuation">,</span> y<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> r1 <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>y<span class="token punctuation">.</span>first<span class="token punctuation">,</span> x<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>r0 <span class="token operator">!=</span> r1<span class="token punctuation">)</span> <span class="token keyword">return</span> r0 <span class="token operator">&lt;</span> r1<span class="token punctuation">;</span>
	<span class="token keyword">return</span> x<span class="token punctuation">.</span>first <span class="token operator">&lt;</span> y<span class="token punctuation">.</span>first<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="C-三-1"><a href="#C-三-1" class="headerlink" title="C. 三"></a>C. 三</h3><div class="note info"><p>给定 $N$ 个数，$M$ 次操作，操作有以下四种：</p>
<ul>
<li>1、区间加一个数。</li>
<li>2、区间乘一个数。</li>
<li>3、区间变成一个数。</li>
<li>4、求所有子区间的平均值的和($MOD = 10^9 + 7$)。<br>$a_i, N,Q \le 10^5$</li>
</ul>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>$Ans = \sum_{i=1}^{n}\limits{\sum_{j=i}^{n}\limits{\frac{\sum_{k=i}^{j}\limits{a_k}}{j-i+1}}}$<br>考虑每个数字的贡献<br>$Ans = \sum_{k=1}^{n}\limits{a_k \times \sum_{i=1}^{n}\limits{\sum_{j=i}^{n}\limits{\frac{1}{j-i+1}}}}$<br>算出贡献，线段树维护即可。可以考虑 $k$ 每增加 1 贡献会变化多少。也可以发现贡献类似于一个梯形，可以直接算。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> cerr <span class="token operator">&lt;&lt;</span> <span class="token string">"FUCK"</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
	S<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> S<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>S<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token function">inv</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> dp <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> last <span class="token operator">=</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> dp <span class="token operator">=</span> <span class="token punctuation">(</span>dp <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> S<span class="token punctuation">[</span>last<span class="token punctuation">]</span> <span class="token operator">+</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		dp <span class="token operator">=</span> <span class="token punctuation">(</span>dp <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> <span class="token punctuation">(</span>S<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> S<span class="token punctuation">[</span>n <span class="token operator">-</span> last<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		dp <span class="token operator">=</span> <span class="token punctuation">(</span>dp <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>S<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> S<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		dp <span class="token operator">=</span> <span class="token punctuation">(</span> dp <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> S<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span> <span class="token comment">// 这里的 dp 就是位置为 i 的数字对答案贡献的系数。</span>
		ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="D-四-1"><a href="#D-四-1" class="headerlink" title="D. 四"></a>D. 四</h3><div class="note info"><p>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。</p>
<p>皮克敏们打完工了，是时候将所有皮克敏处理掉了。现在皮克敏们躲在一棵树上（点和边的任意位置都有可能），你可以选择若干叶子节点释放毒气，毒气会以每单位时间一单位的距离沿着边蔓延开来。为了能够灭绝皮克敏，你需要保证树上每个位置都充满了毒气。但是仅仅是求一个最小的灭绝皮克敏的时间实在太无趣了，你想要知道你有多少种不同的方法能够灭绝皮克敏，两个方法不同当且仅当两种方法灭绝所有皮克敏的时间不同。</p>
<p>$N \le 200$<br>第一行一个整数 $N$ 代表树上点的个数。</p>
<p>接下来 $N−1$ 行每行三个整数 $s,e,d$ 代表一条边的两端和长度。</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>答案只能是某两个叶子之间的距离或者距离 $ / 2$ 。<br>枚举两个叶子，把 </p>
<ul>
<li>这个叶子之间的距离（只在一边放毒气）（如果能成为答案）</li>
<li>两个叶子之间距离 $ / 2$（两个叶子上都放毒气）（如果能成为答案）<br>加入答案集合。 最后输出答案集合的大小。</li>
</ul>
<p>枚举两个叶子，判断其距离是否能成为答案。考虑哪些其他叶子放毒气能让这个距离尽可能成为答案，就是那些放上毒气不会影响这两个枚举的叶子之间毒气传播时间的叶子都放上毒气，然后跑一遍最短路，算全树被毒气覆盖的时间是否等于当前枚举的叶子。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">510</span><span class="token punctuation">;</span>
pair<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> E<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span>
<span class="token keyword">int</span> M<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
set<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Ans<span class="token punctuation">;</span>
queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span>Q<span class="token punctuation">;</span> <span class="token keyword">bool</span> inQ<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">SPFA</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">static</span> <span class="token keyword">int</span> dis<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">memset</span><span class="token punctuation">(</span>dis<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>inQ<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>Q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> now <span class="token operator">=</span> Q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> inQ<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span>i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> 
			<span class="token keyword">if</span><span class="token punctuation">(</span>dis<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">+</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w <span class="token operator">&lt;</span> dis<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
				dis<span class="token punctuation">[</span>ex<span class="token punctuation">]</span> <span class="token operator">=</span> dis<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">+</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>inQ<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">)</span> Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>ex<span class="token punctuation">)</span><span class="token punctuation">,</span> inQ<span class="token punctuation">[</span>ex<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> MAX <span class="token operator">=</span> INT_MIN<span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		pair<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> now <span class="token operator">=</span> E<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> T <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>dis<span class="token punctuation">[</span>now<span class="token punctuation">.</span>first<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">+</span> now<span class="token punctuation">.</span>second<span class="token punctuation">,</span> dis<span class="token punctuation">[</span>now<span class="token punctuation">.</span>first<span class="token punctuation">.</span>second<span class="token punctuation">]</span> <span class="token operator">+</span> now<span class="token punctuation">.</span>second<span class="token punctuation">,</span> <span class="token punctuation">(</span>dis<span class="token punctuation">[</span>now<span class="token punctuation">.</span>first<span class="token punctuation">.</span>first<span class="token punctuation">]</span> <span class="token operator">+</span> dis<span class="token punctuation">[</span>now<span class="token punctuation">.</span>first<span class="token punctuation">.</span>second<span class="token punctuation">]</span> <span class="token operator">+</span> now<span class="token punctuation">.</span>second<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		MAX <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>MAX<span class="token punctuation">,</span> T<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> MAX<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> ind<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">work1</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">int</span> D<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> inQ<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">==</span> x <span class="token operator">||</span> i <span class="token operator">==</span> y<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">>=</span> D <span class="token operator">&amp;&amp;</span> ind<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span> inQ<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token function">SPFA</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> D<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">bool</span> <span class="token function">work2</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">int</span> D<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> inQ<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> inQ<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">==</span> i <span class="token operator">||</span> y <span class="token operator">==</span> i<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">max</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> M<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span> <span class="token operator">>=</span> D <span class="token operator">&amp;&amp;</span> ind<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> Q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span> inQ<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token function">SPFA</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> D<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>M<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>M<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span> ind<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">++</span><span class="token punctuation">;</span> ind<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span> w <span class="token operator">&lt;&lt;=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> u<span class="token punctuation">,</span> w<span class="token punctuation">)</span><span class="token punctuation">;</span> M<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">=</span> M<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> w<span class="token punctuation">;</span> E<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">,</span> w<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> M<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">+</span> M<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">==</span> j<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ind<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">1</span> <span class="token operator">||</span> ind<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>Ans<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>      <span class="token operator">&amp;&amp;</span> <span class="token function">work1</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>      Ans<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>Ans<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">work2</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> Ans<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>M<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">(</span>Ans<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h2 id="Round-6"><a href="#Round-6" class="headerlink" title="Round 6"></a>Round 6</h2><h3 id="A-一-2"><a href="#A-一-2" class="headerlink" title="A. 一"></a>A. 一</h3><div class="note info"><p>现在我们有两队皮克敏，个数分别为$n_1,n_2$，现在我们要杀掉这些皮克敏，按照如下规则操作：对于第$i$轮杀皮克敏的操作，我们首先选择皮克敏较多的那一队，如果一样就选择第一队，然后杀掉这队中的$i$个皮克敏，如果不够，游戏结束，记做游戏在第$i$轮结束。问最后两队各剩下多少个皮克敏？<br>$n \le 10^{16}$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>分成两阶段，一阶段是只杀多的那队，直到比另一队少。二阶段是两队交替着杀，可以证明，如果经过了第一阶段，那么第二阶段一定是交替着杀。 <del>杀就完了。</del> 分别二分即可，当然第一步也可以直接解方程。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">LL A<span class="token punctuation">,</span> B<span class="token punctuation">;</span>
<span class="token keyword">inline</span> LL <span class="token function">check</span><span class="token punctuation">(</span>LL x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> x <span class="token operator">*</span> <span class="token punctuation">(</span>x <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
LL <span class="token function">Get</span><span class="token punctuation">(</span>LL res<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	LL L <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> R <span class="token operator">=</span> <span class="token number">1e9</span><span class="token punctuation">,</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
	<span class="token keyword">while</span><span class="token punctuation">(</span>L <span class="token operator">&lt;</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		LL mid <span class="token operator">=</span> L <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>R <span class="token operator">-</span> L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">check</span><span class="token punctuation">(</span>mid<span class="token punctuation">)</span> <span class="token operator">&lt;=</span> res<span class="token punctuation">)</span> ans <span class="token operator">=</span> mid<span class="token punctuation">,</span> L <span class="token operator">=</span> mid<span class="token punctuation">;</span>
		<span class="token keyword">else</span> R <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
LL Start <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> LL tA<span class="token punctuation">,</span> tB<span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">check0</span><span class="token punctuation">(</span>LL Round<span class="token punctuation">,</span> LL A<span class="token punctuation">,</span> LL B<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	LL dA<span class="token punctuation">,</span> dB<span class="token punctuation">;</span> <span class="token keyword">bool</span> sf <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>A <span class="token operator">&lt;</span> B<span class="token punctuation">)</span> sf <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token function">swap</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>Round <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> 
		dA <span class="token operator">=</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> Round <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>Round <span class="token operator">/</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> 
		dB <span class="token operator">=</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> Round <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>Round <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
		dA <span class="token operator">=</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> Round <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>Round <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> 
		dB <span class="token operator">=</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token punctuation">(</span>Start <span class="token operator">+</span> Round <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>Round <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> A <span class="token operator">-=</span> dA<span class="token punctuation">;</span> B <span class="token operator">-=</span> dB<span class="token punctuation">;</span> 
	<span class="token keyword">if</span><span class="token punctuation">(</span>sf<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
	tA <span class="token operator">=</span> A<span class="token punctuation">;</span> tB <span class="token operator">=</span> B<span class="token punctuation">;</span>
	<span class="token keyword">return</span> A <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> B <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">doit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">)</span><span class="token punctuation">(</span>B<span class="token punctuation">)</span><span class="token punctuation">;</span>
	LL d <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">min</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
	LL Round <span class="token operator">=</span> <span class="token function">Get</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span> LL del <span class="token operator">=</span> <span class="token function">check</span><span class="token punctuation">(</span>Round<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>A <span class="token operator">&lt;</span> B<span class="token punctuation">)</span> B <span class="token operator">-=</span> del<span class="token punctuation">;</span> <span class="token keyword">else</span> A <span class="token operator">-=</span> del<span class="token punctuation">;</span> 
	<span class="token keyword">if</span><span class="token punctuation">(</span>A <span class="token operator">==</span> B<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>A <span class="token operator">&lt;</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld %lld %lld\n"</span><span class="token punctuation">,</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> Round <span class="token operator">++</span><span class="token punctuation">,</span> A <span class="token operator">-=</span> Round<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">max</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span> B<span class="token punctuation">)</span> <span class="token operator">&lt;</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld %lld %lld\n"</span><span class="token punctuation">,</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> Round <span class="token operator">++</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>A <span class="token operator">></span> B <span class="token operator">?</span> A <span class="token operator">-=</span> Round <span class="token operator">:</span> B <span class="token operator">-=</span> Round<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	LL L <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> R <span class="token operator">=</span> <span class="token number">1e9</span><span class="token punctuation">,</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> Start <span class="token operator">=</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>L <span class="token operator">&lt;</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		LL mid <span class="token operator">=</span> L <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>R <span class="token operator">-</span> L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">check0</span><span class="token punctuation">(</span>mid<span class="token punctuation">,</span> A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">)</span> ans <span class="token operator">=</span> mid<span class="token punctuation">,</span> L <span class="token operator">=</span> mid<span class="token punctuation">;</span>
		<span class="token keyword">else</span> R <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token function">check0</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> A<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld %lld %lld\n"</span><span class="token punctuation">,</span> ans <span class="token operator">+</span> Round <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> tA<span class="token punctuation">,</span> tB<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> T <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cerr <span class="token operator">&lt;&lt;</span> <span class="token string">"std's T = "</span> <span class="token operator">&lt;&lt;</span> T <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>T<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token function">doit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="B-二-2"><a href="#B-二-2" class="headerlink" title="B. 二"></a>B. 二</h3><div class="note info"><p>现在有三组N个数记做$A,B,C$，定义函数$f(A,B)=\sum_{i=1}^NA_i\times B_i$。现在给定$A,B$，并告诉你$f(A,C)=x$，现在想求在满足$∑_{i=1}^NC_i=1,0\leq C_i\leq1$的情况下$f(B,C)$的最大值。<br>对于$100%$的数据，$1\leq N,M\leq 100,1\leq A_i,B_i\leq100$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>[skip] 结论是只有两个点有用，只枚举两个点即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</details>
<h3 id="C-三-2"><a href="#C-三-2" class="headerlink" title="C. 三"></a>C. 三</h3><div class="note info"><p>众所周知，小葱同学擅长计算，尤其擅长计算组合数，但这个题和组合数没什么关系。</p>
<p>现在有一个 $N$ 的排列，将其每个数看做一个集合。定义一种对两个集合的运算为：$f(s_1,s_2 )=\sum_{x\in s_1}[\exists y\in s_2,y &lt; x]$<br>也可以用这样一段伪代码去理解：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">Ans<span class="token operator">=</span><span class="token number">0</span>
For x <span class="token keyword">in</span> s1<span class="token punctuation">:</span>
    Able <span class="token operator">=</span> <span class="token boolean">False</span>
    For y <span class="token keyword">in</span> s2<span class="token punctuation">:</span>
        If y<span class="token operator">&lt;</span>x<span class="token punctuation">:</span>
            Able<span class="token operator">=</span><span class="token boolean">True</span>
    If Able<span class="token punctuation">:</span>
        Ans<span class="token operator">+</span><span class="token operator">+</span>
Return Ans<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>现在你每次可以合并两个相邻的集合将其变为两个集合的并集，其代价为$f(s_1,s_2)+f(s_2,s_1)$。求最小代价，将所有集合合并为一个集合。<br>$N \le 10^2$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>函数 $f(A, B)$ 其实就是 $A$ 中有多少元素大于 $\min{B_i}$ 。按照定义区间 dp 即可，由于是小于 $\mathcal{O}(n^2)$ 级别的二维数点，可以使用二维前缀和。 查询最小值随手预处理一下 <code>ST</code> 表即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">600</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> S<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> <span class="token keyword">int</span> up<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">// how many elements in range[L, R] which is > up </span>
	<span class="token keyword">return</span> S<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">[</span>R<span class="token punctuation">]</span> <span class="token operator">-</span> S<span class="token punctuation">[</span>up<span class="token punctuation">]</span><span class="token punctuation">[</span>R<span class="token punctuation">]</span> <span class="token operator">-</span> S<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> S<span class="token punctuation">[</span>up<span class="token punctuation">]</span><span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _S <span class="token operator">=</span> <span class="token number">4000</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> LOG <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> ST<span class="token punctuation">[</span>_S<span class="token punctuation">]</span><span class="token punctuation">[</span>LOG <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> Log<span class="token punctuation">[</span>_S<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">initQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> LOG<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ST<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ST<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token punctuation">(</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	Log<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> Log<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> Log<span class="token punctuation">[</span>i <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">RMQ</span><span class="token punctuation">(</span><span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> Ln <span class="token operator">=</span> Log<span class="token punctuation">[</span>R <span class="token operator">-</span> L <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token function">min</span><span class="token punctuation">(</span>ST<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">[</span>Ln<span class="token punctuation">]</span><span class="token punctuation">,</span> ST<span class="token punctuation">[</span>R <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> Ln<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>Ln<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">calc</span><span class="token punctuation">(</span><span class="token keyword">int</span> L0<span class="token punctuation">,</span> <span class="token keyword">int</span> R0<span class="token punctuation">,</span> <span class="token keyword">int</span> L1<span class="token punctuation">,</span> <span class="token keyword">int</span> R1<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> min0 <span class="token operator">=</span> <span class="token function">RMQ</span><span class="token punctuation">(</span>L0<span class="token punctuation">,</span> R0<span class="token punctuation">)</span><span class="token punctuation">,</span> min1 <span class="token operator">=</span> <span class="token function">RMQ</span><span class="token punctuation">(</span>L1<span class="token punctuation">,</span> R1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span>L0<span class="token punctuation">,</span> R0<span class="token punctuation">,</span> min1<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">query</span><span class="token punctuation">(</span>L1<span class="token punctuation">,</span> R1<span class="token punctuation">,</span> min0<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">initQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> S<span class="token punctuation">[</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> S<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">+=</span> S<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">+</span> S<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> S<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>Len<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> R <span class="token operator">=</span> L <span class="token operator">+</span> Len <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>R <span class="token operator">></span> n<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> <span class="token operator">&amp;</span>ans <span class="token operator">=</span> dp<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">[</span>R<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> dp<span class="token punctuation">[</span>L<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">+</span> dp<span class="token punctuation">[</span>k <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>R<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token function">calc</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> k<span class="token punctuation">,</span> k <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="D-四-2"><a href="#D-四-2" class="headerlink" title="D. 四"></a>D. 四</h3><div class="note info"><p>给定 $1-N$ 的排列但其中 $M$ 个位置的值被删去了（用 $0$ 表示），现在你需要将其复原，问有多少种方案能够使得复原的序列的逆序对个数在 $[L,R]$ 之间</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>折半搜索，中间 $\mathcal{O}(n^2)$ 合并。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</details>


<h2 id="Round-7"><a href="#Round-7" class="headerlink" title="Round 7"></a>Round 7</h2><p>AK。</p>
<h3 id="A-一-3"><a href="#A-一-3" class="headerlink" title="A. 一"></a>A. 一</h3><div class="note info"><p>有一个$n\times n$的矩阵，矩阵中的每个数都是整数。现在要从矩阵中取$m$个数，要求每一行最多取一个数，每一列也最多取一个数。这$m$个数的和最大能是多少？<br>$n \le 15, m \le 3, A_{i,j} \le 10^5$ </p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> MAX <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> MAX <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>MAX<span class="token punctuation">,</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> MAX <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>d<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>a <span class="token operator">==</span> c <span class="token operator">||</span> b <span class="token operator">==</span> d<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			MAX <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>MAX<span class="token punctuation">,</span> A<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">+</span> A<span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">[</span>d<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> MAX <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>d<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>e<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>a <span class="token operator">==</span> c <span class="token operator">||</span> c <span class="token operator">==</span> e <span class="token operator">||</span> a <span class="token operator">==</span> e <span class="token operator">||</span> b <span class="token operator">==</span> d <span class="token operator">||</span> d <span class="token operator">==</span> f <span class="token operator">||</span> b <span class="token operator">==</span> f<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			MAX <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>MAX<span class="token punctuation">,</span> A<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">+</span> A<span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">[</span>d<span class="token punctuation">]</span> <span class="token operator">+</span> A<span class="token punctuation">[</span>e<span class="token punctuation">]</span><span class="token punctuation">[</span>f<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="B-二-3"><a href="#B-二-3" class="headerlink" title="B. 二"></a>B. 二</h3><div class="note info"><p>有多少整数大于等于$x$，小于等于$y$，而且是7的倍数，又不是2,3,5的倍数呢？<br>$x,y \le 10^{18}$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>容斥原理</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">LL <span class="token function">calc</span><span class="token punctuation">(</span>LL R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	LL ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	ans <span class="token operator">+=</span> R <span class="token operator">/</span> <span class="token number">7</span><span class="token punctuation">;</span> ans <span class="token operator">-=</span> R <span class="token operator">/</span> <span class="token number">14</span><span class="token punctuation">;</span> ans <span class="token operator">-=</span> R <span class="token operator">/</span> <span class="token number">21</span><span class="token punctuation">;</span> ans <span class="token operator">-=</span> R <span class="token operator">/</span> <span class="token number">35</span><span class="token punctuation">;</span> ans <span class="token operator">+=</span> R <span class="token operator">/</span> <span class="token number">42</span><span class="token punctuation">;</span> ans <span class="token operator">+=</span> R <span class="token operator">/</span> <span class="token number">70</span><span class="token punctuation">;</span> ans <span class="token operator">+=</span> R <span class="token operator">/</span> <span class="token number">105</span><span class="token punctuation">;</span> ans <span class="token operator">-=</span> R <span class="token operator">/</span> <span class="token number">210</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	LL L<span class="token punctuation">,</span> R<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>L<span class="token punctuation">)</span><span class="token punctuation">(</span>R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld"</span><span class="token punctuation">,</span> <span class="token function">calc</span><span class="token punctuation">(</span>R<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">calc</span><span class="token punctuation">(</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="C-三-3"><a href="#C-三-3" class="headerlink" title="C. 三"></a>C. 三</h3><div class="note info"><p>给出一个$n$个点$m$条边的图。</p>
<p>现在想要选择两个整数$a$和$b$，满足$1 \leq a &lt; b &lt; n$，然后把编号在区间$[1,a]$中的点染成蓝色，把编号在区间$[a+1,b]$中的点染成红色，编号在区间$[b+1,n]$中的点染成蓝色。</p>
<p>如果一条边的两个端点颜色不同，我们称这条边为双色边，否则称之为单色边。</p>
<p>如何选择$a,b$，能够最小化双色边的数量？<br>$n, m \le 10^5$</p>
</div>

<details class="note success"><summary><p>Solution</p>
</summary>
<p>经典套路，按顺序枚举 $a$，线段树维护 $b$ 的每一个取值时的双色边数量，枚举 $a$ 的过程中维护 $b$ 的取值，更新答案即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">3e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> ToL<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span> 
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> ToR<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">namespace</span> SegmentTree<span class="token punctuation">&#123;</span>	<span class="token comment">// maintain the number of double-color edge;</span>
					  	<span class="token comment">// the min number ;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ch<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> v<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">make</span> <span class="token punctuation">(</span>tot<span class="token operator">++</span><span class="token punctuation">,</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> v<span class="token punctuation">[</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> tag_add<span class="token punctuation">[</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tot<span class="token punctuation">)</span></span></span>
	<span class="token keyword">int</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> make<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span> <span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span>
		<span class="token function">build</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">tar</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> _v<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">+=</span> _v<span class="token punctuation">;</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">+=</span> _v<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token function">tar</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">tar</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> tag_add<span class="token punctuation">[</span>o<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> <span class="token keyword">int</span> Val<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">tar</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">push</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> ans <span class="token operator">=</span> INT_MAX<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> 	<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update<span class="token punctuation">;</span> 	<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>build<span class="token punctuation">;</span> 
  	<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>query<span class="token punctuation">;</span>	<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>Groot<span class="token punctuation">;</span> 
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> 
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> root <span class="token operator">=</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ans <span class="token operator">=</span> INT_MAX<span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>u <span class="token operator">></span> v<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		ToL<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span> ToR<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> u<span class="token punctuation">,</span> v <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token keyword">int</span><span class="token punctuation">(</span>ToL<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> ToR<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> i<span class="token punctuation">,</span> ToR<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> ToR<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="D-四-3"><a href="#D-四-3" class="headerlink" title="D. 四"></a>D. 四</h3><div class="note info"><p>$n$个小伙伴（编号从0到$n−1$）围坐一圈玩游戏。按照顺时针方向给$n$个位置编号，从0到$n−1$。最初，第0号小伙伴在第0号位置，第1号小伙伴在第1号位置，$\dots$，依此类推。游戏规则如下：</p>
<ul>
<li>有一个序列$A={a_1,\dots,a_m}$，其中每个数互不相同，且都是$[1,n-1]$中的正整数。</li>
<li>每一轮从$A$中选择一个数$a$，第0号位置上的小伙伴顺时针走到第$a$号位置，第1号位置小伙伴走到第$a+1$号位置。依此类推，第$n-a$号位置上的小伙伴走到第0号位置，第$n-a+1$号位置上的小伙伴走到第1号位置，$\dots$，第$n-1$号位置上的小伙伴顺时针走到第$a-1$号位置。也就是说，第$i$号位置上的小伙伴走到第$(i+a)\mod\ n$号位置</li>
</ul>
<p>游戏进行$t$轮。由于每一轮都需要从$A$中的$m$个数中选一个数，所以游戏共有$m^t$种玩法。其中有多少种玩法能够使得游戏结束时，第0号小伙伴所在的位置编号是$d$的倍数？（0也是$d$的倍数）、<br>对 $10^9 + 7$取模。<br>$1\le m \le n \le 1000, 1 \le t \le 10^9, 1 \le d \le n$。 </p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>设 $dp[i]$ 为进行若干轮之后在位置 $i$ 的方案数。 转移显然是卷积。 包装了循环卷积的定义。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">namespace</span> subtask_acceptable_solution<span class="token punctuation">&#123;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">1500</span><span class="token punctuation">;</span>
	<span class="token keyword">struct</span> <span class="token class-name">Matrix</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> dp<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token function">Matrix</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		Matrix <span class="token keyword">operator</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Matrix <span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			Matrix Res<span class="token punctuation">;</span> 
			<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> Res<span class="token punctuation">.</span>dp<span class="token punctuation">[</span><span class="token punctuation">(</span>i <span class="token operator">+</span> j<span class="token punctuation">)</span> <span class="token operator">%</span> n<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>Res<span class="token punctuation">.</span>dp<span class="token punctuation">[</span><span class="token punctuation">(</span>i <span class="token operator">+</span> j<span class="token punctuation">)</span> <span class="token operator">%</span> n<span class="token punctuation">]</span> <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> rhs<span class="token punctuation">.</span>dp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">return</span> Res<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	Matrix <span class="token function">pow</span><span class="token punctuation">(</span>Matrix a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Matrix ans <span class="token operator">=</span> a<span class="token punctuation">;</span> b<span class="token operator">--</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>b <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span> ans <span class="token operator">=</span> ans <span class="token operator">*</span> a<span class="token punctuation">;</span>
			a <span class="token operator">=</span> a <span class="token operator">*</span> a<span class="token punctuation">;</span>
			b <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Matrix a<span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span> a<span class="token punctuation">.</span>dp<span class="token punctuation">[</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">++</span><span class="token punctuation">;</span>
		Matrix Ans <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i <span class="token operator">+=</span> d<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> Ans<span class="token punctuation">.</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">//freopen("in.txt", "r", stdin);</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">%</span> n<span class="token punctuation">;</span>
	subtask_acceptable_solution<span class="token operator">::</span><span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>


<h2 id="Round-8"><a href="#Round-8" class="headerlink" title="Round 8"></a>Round 8</h2><h3 id="A-双挂"><a href="#A-双挂" class="headerlink" title="A. 双挂"></a>A. 双挂</h3><div class="note info"><p>今年期中考试考了《程序设计》、《算法设计》和《数据结构》共三门课。由于考试太难了，好多同学都挂科了。班里一共$n$个人，其中$a$个人挂了《程序设计》，$b$个人挂了《算法设计》，$c$个人挂了《数据结构》。</p>
<p>如果一个同学挂了恰好两门课，那么我们就说他“双挂”了。<br>给出$n,a,b,c$，问最少有多少同学“双挂”了。</p>
<p>$0 \le a, b, c \le n, n \le 10^9$</p>
</div>
<details class="note success"><summary><p>solution</p>
</summary>
<p>设三门课挂科的人对应集合分别为 $\mathbb{A}, \mathbb{B}, \mathbb{C}$</p>
<p>显然并不是所有人都会挂科目。</p>
<p>$|\mathbb{A} \cup \mathbb{B} \cup \mathbb{C}| = |\mathbb{A}| + |\mathbb{B}| + |\mathbb{C}| - |\mathbb{A} \cap \mathbb{B}|  - |\mathbb{A} \cap \mathbb{C}| - |\mathbb{B} \cap \mathbb{C}| + |\mathbb{A} \cap \mathbb{B} \cap \mathbb{C}| \le n$</p>
<p>移项得</p>
<p>为了方便：设 $S = \mathbb{A} \cap \mathbb{B} \cap \mathbb{C}$</p>
<p>$\left(|\mathbb{A} \cap \mathbb{B}| - |S|\right) + \left(|\mathbb{A} \cap \mathbb{C}| - |S|\right) + \left(|\mathbb{B} \cap \mathbb{C}| - |S|\right) \ge |\mathbb{A}| + |\mathbb{B}| + |\mathbb{C}| + |S| - n - 3|S|$</p>
<p>答案取值为 $|\mathbb{A}| + |\mathbb{B}| + |\mathbb{C}| -2|S| - n$，其中 $|S|$ 越大 答案越小，其最大取值就是 $\max{ { \mathbb{A}, \mathbb{B}, \mathbb{C} } }$</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">LL n<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld"</span><span class="token punctuation">,</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token number">0LL</span><span class="token punctuation">,</span> a <span class="token operator">+</span> b <span class="token operator">+</span> c <span class="token operator">-</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token function">min</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
</details>
<h3 id="B-DDL选手"><a href="#B-DDL选手" class="headerlink" title="B. DDL选手"></a>B. DDL选手</h3><div class="note info"><p>DDL选手总是在最后做作业。</p>
<p>有$n$个作业，第$i$个作业的DDL时间是$d_i$，做这个作业需要$t_i$的时间。</p>
<p>做作业要一心一意，所以不能同时做两个作业。如果现在开始做第$i$个作业，那么接下来$t_i$的时间都要做这个作业，不能做别的。</p>
<p>DDL选手总是先做DDL时间早的作业。如果两个作业的DDL时间一样，那么DDL选手会先做其中编号小的</p>
<p>为DDL选手设计每个作业开始做的时间，使得DDL选手先做DDL早的作业（如果两个作业的DDL时间一样，那么先做其中编号小的），而且每个作业都能在DDL时间之前做完（时间是连续的，“之前”的意思是，最晚恰好这个时间完成。）。在保证完成的前提下，尽可能把开始做作业的时间往后推。（也就是如果有多个方案可以做完作业，选择开始时间最晚的方案。可以证明，最优方案中，每一个作业的开始时间都不早于其他方案的开始时间。输出这个最优方案。）</p>
<p>$n \le 10^5, t_i \le 10^4$</p>
</div>
<details class="note success"><summary><p>solution</p>
</summary>
<p>依照题意反向模拟。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token keyword">int</span> <span class="token keyword">long</span> <span class="token keyword">long</span> </span></span>
<span class="token keyword">struct</span> <span class="token class-name">HomeWork_t</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> d<span class="token punctuation">,</span> t<span class="token punctuation">,</span> id<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>H<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> n <span class="token punctuation">;</span>
<span class="token keyword">int</span> Ans<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">CMP</span><span class="token punctuation">(</span><span class="token keyword">const</span> HomeWork_t <span class="token operator">&amp;</span> A<span class="token punctuation">,</span> <span class="token keyword">const</span> HomeWork_t <span class="token operator">&amp;</span> B<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>A<span class="token punctuation">.</span>d <span class="token operator">&lt;</span> B<span class="token punctuation">.</span>d<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>A<span class="token punctuation">.</span>d <span class="token operator">==</span> B<span class="token punctuation">.</span>d <span class="token operator">&amp;&amp;</span> A<span class="token punctuation">.</span>id <span class="token operator">&lt;</span> B<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">undef</span> <span class="token expression"><span class="token keyword">int</span> </span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>d<span class="token punctuation">)</span><span class="token punctuation">(</span>H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>t<span class="token punctuation">)</span><span class="token punctuation">,</span> H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id <span class="token operator">=</span> i<span class="token punctuation">;</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>H <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> H <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">+</span> n<span class="token punctuation">,</span> CMP<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token keyword">int</span> <span class="token keyword">long</span> <span class="token keyword">long</span> </span></span>
	<span class="token keyword">int</span> Last <span class="token operator">=</span> LLONG_MAX<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n<span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>Last <span class="token operator">>=</span> H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>d<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			Last <span class="token operator">=</span> H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>d <span class="token operator">-</span> H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>t<span class="token punctuation">;</span>
			Ans<span class="token punctuation">[</span>H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id<span class="token punctuation">]</span> <span class="token operator">=</span> Last<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
			Last <span class="token operator">=</span> <span class="token punctuation">(</span>Last<span class="token punctuation">)</span> <span class="token operator">-</span> H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>t<span class="token punctuation">;</span>
			Ans<span class="token punctuation">[</span>H<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id<span class="token punctuation">]</span> <span class="token operator">=</span> Last<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld\n"</span><span class="token punctuation">,</span> Ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">undef</span> <span class="token expression"><span class="token keyword">int</span></span></span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h3 id="C-约数链"><a href="#C-约数链" class="headerlink" title="C. 约数链"></a>C. 约数链</h3><div class="note info"><p>如果$n$个数$a_1,\dots,a_n$满足：对于$1\leq i &lt; n$，$a_i$是$a_{i+1}$的约数，那么$a_1,\dots,a_n$被称为一个约数链。一个约数链的权值是它所包含的$n$个数的乘积。</p>
<p>给出$n,m$，只允许使用不超过$m$的正整数组成长为$n$的约数链，会有非常多的方式。求所有这些方式得到的约数链的权值之和。<br>$n \le 10, m \le 10^7$</p>
</div>
<details class="note success"><summary><p>solution</p>
</summary>
<p>可以 <strong>dp</strong> 一下，设 $dp[n][k]$ 为长度为 $n$ 的约数链，结尾是 $k$ 的权值和。 转移为刷表。复杂度做到 $\mathcal{O}(nm\log m)$。根本过不去。</p>
<p>设 $f_n(k) = dp[n][k]$ 其中 函数 $f_k(x)$ 为积性函数。</p>
<p>对于 $a \perp b$ 设 $c = a \times b$ 对于 $f_n(c)$ 所统计的方案中的每一个数字都可以质因数分解成两组，一组是只属于 $a$ 的质因子，另一组是只属于 $b$ 的质因子。手举几个栗子就能发现符合积性函数的性质。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">1e7</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> MOD <span class="token operator">=</span> <span class="token number">1e9</span> <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> np<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> prime<span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">1e6</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> Mid<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">Init</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	Mid<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> np<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>np<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> prime<span class="token punctuation">[</span><span class="token operator">++</span>tot<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">,</span> Mid<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator">&lt;=</span> tot <span class="token operator">&amp;&amp;</span> prime<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> x <span class="token operator">=</span> prime<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> i<span class="token punctuation">;</span>
			np<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> Mid<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> prime<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">%</span> prime<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span>
<span class="token keyword">int</span> dp<span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> Ans<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> t <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">//freopen("in.txt", "r", stdin);</span>
	<span class="token function">Init</span><span class="token punctuation">(</span><span class="token number">1e7</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token function">rep</span><span class="token punctuation">(</span>S<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> tot<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> now <span class="token operator">=</span> prime<span class="token punctuation">[</span>S<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>dp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		t <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> t <span class="token operator">&lt;=</span> m<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">,</span> t <span class="token operator">*=</span> now<span class="token punctuation">)</span> dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> t<span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			t <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> t <span class="token operator">&lt;=</span> m<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">,</span> t <span class="token operator">*=</span> now<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
				<span class="token keyword">int</span> <span class="token operator">&amp;</span>ans <span class="token operator">=</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
				<span class="token function">rep</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> j<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> t <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		t <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> t <span class="token operator">&lt;=</span> m<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">,</span> t <span class="token operator">*=</span> now<span class="token punctuation">)</span> Ans<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	Ans<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>Ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> A <span class="token operator">=</span> i<span class="token punctuation">,</span> B <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>A <span class="token operator">%</span> Mid<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> A <span class="token operator">/=</span> Mid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> B <span class="token operator">*=</span> Mid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		Ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>Ans<span class="token punctuation">[</span>A<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> Ans<span class="token punctuation">[</span>B<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span> ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> Ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="D-连通"><a href="#D-连通" class="headerlink" title="D. 连通"></a>D. 连通</h3><div class="note info"><p>给出一张无向图，求有多少种方案使得删除三条边，使原图不连通。</p>
<p>$n,m \le 2000$</p>
</div>

<details class="note success"><summary><p>solution</p>
</summary>
<p>考虑枚举删除的第一条边是那条，然后问题转化成了 求在一张图中删除两条边使图不连通的方案数。<br>先删去一条边，考虑剩下的图的形态，</p>
<ul>
<li>图已经不连通了，那答案的贡献就是每条边两两配对的方案数</li>
<li>图是联通的，考虑对图建一个生成树，考虑删边的方式<ul>
<li>删除两条非树边 一定不合法，之前的树仍然存在，保证了图的连通性。</li>
<li>删除一条树边，一条非树边 被计入答案，当且仅当，这条树边被这条非树边唯一覆盖。这里的正确性显然。</li>
<li>删除两条非树边 被计入答案，当且仅当，这两条边被相同的一组非树边覆盖。<ul>
<li>证明可以考虑，对于原树来说，如果删除两条树边，断成三份，如果两条边被相同的一组非树边覆盖，没有边可以联通断开的中间部分。</li>
</ul>
</li>
</ul>
</li>
<li>考虑如何实现，对每一条非树边赋一个随机权值，然后树边的权值定义为覆盖其的每一条非树边的权值异或和。统计删除两条边后图不联通的方案数，就转化为，统计有多少种方案选出两条边，使其权值相同。 树上差分即可，异或有自反性，其标记在 LCA 处自动消失。</li>
</ul>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">4100</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> head<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edges</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> node<span class="token punctuation">;</span>
	<span class="token keyword">int</span> nxt<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>edge<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> tot <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> MOD <span class="token operator">=</span> <span class="token number">1e9</span> <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span>
<span class="token keyword">int</span> ToEid<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> ToNid<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> book<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
u64 tag<span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">2100</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">2100</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
u64 EVal<span class="token punctuation">[</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">2100</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	tot<span class="token operator">++</span><span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>node <span class="token operator">=</span> v<span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt  <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span>        <span class="token operator">=</span> tot<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
u64 <span class="token function">gen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	u64 A <span class="token operator">=</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">1ull</span><span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">1ull</span><span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	u64 B <span class="token operator">=</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">1ull</span><span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">1ull</span><span class="token operator">*</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> A <span class="token operator">+</span> B <span class="token operator">+</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token keyword">int</span> now<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	vis<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token operator">==</span> f<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">dfs0</span><span class="token punctuation">(</span>ex<span class="token punctuation">,</span> now<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> <span class="token punctuation">&#123;</span> 
			<span class="token keyword">if</span><span class="token punctuation">(</span> EVal<span class="token punctuation">[</span>ToNid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			u64 g <span class="token operator">=</span> EVal<span class="token punctuation">[</span>ToNid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">gen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			tag<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">^=</span> g<span class="token punctuation">;</span> tag<span class="token punctuation">[</span>ex<span class="token punctuation">]</span> <span class="token operator">^=</span> g<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token keyword">int</span> now<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	u64 <span class="token operator">&amp;</span>ntag <span class="token operator">=</span> tag<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token operator">==</span> f<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>EVal<span class="token punctuation">[</span>ToNid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">dfs1</span><span class="token punctuation">(</span>ex<span class="token punctuation">,</span> now<span class="token punctuation">)</span><span class="token punctuation">,</span> EVal<span class="token punctuation">[</span>ToNid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> tag<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">,</span> ntag <span class="token operator">^=</span> tag<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">;</span> 
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
map<span class="token operator">&lt;</span>u64<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> M<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> inv6 <span class="token operator">=</span> <span class="token number">166666668</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">// freopen("in.txt", "r", stdin);</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> <span class="token operator">*</span>seed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">;</span> <span class="token function">srand</span><span class="token punctuation">(</span><span class="token operator">*</span>seed <span class="token operator">+</span> n <span class="token operator">+</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		ToEid<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> tot <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> ToNid<span class="token punctuation">[</span>tot <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> ToNid<span class="token punctuation">[</span>tot <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> u<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> Pans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> 
		<span class="token keyword">int</span> rs <span class="token operator">=</span> ToEid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> 
		book<span class="token punctuation">[</span>rs<span class="token punctuation">]</span> <span class="token operator">=</span> book<span class="token punctuation">[</span>rs <span class="token operator">^</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>tag<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>tag<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">memset</span><span class="token punctuation">(</span>EVal<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>EVal<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> M<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
		
		<span class="token keyword">bool</span> pass <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span>  <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> pass <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>pass<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> t <span class="token operator">=</span> m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> Pans <span class="token operator">=</span> <span class="token punctuation">(</span>Pans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>t <span class="token operator">*</span> <span class="token punctuation">(</span>t <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span> book<span class="token punctuation">[</span>rs<span class="token punctuation">]</span> <span class="token operator">=</span> book<span class="token punctuation">[</span>rs <span class="token operator">^</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
		<span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> m<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">!=</span> j<span class="token punctuation">)</span> M<span class="token punctuation">[</span>EVal<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">++</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>map<span class="token operator">&lt;</span>u64<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">::</span> iterator i <span class="token operator">=</span> M<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> M<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">-></span>first <span class="token operator">==</span> <span class="token number">0ull</span><span class="token punctuation">)</span>
				ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">,</span>
				ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">2ll</span><span class="token operator">*</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
			<span class="token keyword">else</span>
				ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token punctuation">(</span>i<span class="token operator">-></span>second <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		Pans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> Pans<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		book<span class="token punctuation">[</span>rs<span class="token punctuation">]</span> <span class="token operator">=</span> book<span class="token punctuation">[</span>rs <span class="token operator">^</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">(</span>Pans <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> inv6 <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>


<h2 id="Round-9"><a href="#Round-9" class="headerlink" title="Round 9"></a>Round 9</h2><p>降智场。</p>
<h3 id="A-最大公约数"><a href="#A-最大公约数" class="headerlink" title="A. 最大公约数"></a>A. 最大公约数</h3><div class="note info"><p>给出一个长度为 $n$ 的序列。<br>设 $k = \max_{i &lt; j}{\operatorname{gcd}(A_i, A_j)}$，<br>求出：</p>
<ul>
<li>$k$</li>
<li>$\sum_{i &lt; j}\limits{[\operatorname{gcd}(A_i, A_j)=k]}$</li>
<li>$\sum_{i &lt; j}\limits{[\operatorname{gcd}(A_i, A_j)=k]A_i \times A_j}$</li>
</ul>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>枚举一下 $gcd$ 可能以这个数字为 $gcd$ 的两个数字一定是 $gcd$ 的倍数。 从大到小枚举 $gcd$ 查询有多少倍数，如果大于 $2$ ，这个 $gcd$ 就可以。 剩余的两项在得到 $k$ 的基础上判断即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> MOD <span class="token operator">=</span> <span class="token number">998244353</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> inv2 <span class="token operator">=</span> <span class="token number">499122177</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> book<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> A<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> MAX <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token keyword">int</span> K <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> book<span class="token punctuation">[</span>A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">,</span> MAX <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>MAX<span class="token punctuation">,</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> MAX<span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i<span class="token punctuation">;</span> j <span class="token operator">&lt;=</span> MAX<span class="token punctuation">;</span> j <span class="token operator">+=</span> i<span class="token punctuation">)</span> cnt <span class="token operator">+=</span> book<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>cnt <span class="token operator">>=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> K <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> SUM <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> ans1 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> ans2 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> K<span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> MAX<span class="token punctuation">;</span> i <span class="token operator">+=</span> K<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		ans1 <span class="token operator">+=</span> book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> SUM <span class="token operator">=</span> <span class="token punctuation">(</span>SUM <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> i <span class="token operator">%</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ans1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>ans1 <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token punctuation">(</span>ans1 <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> inv2<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> K<span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> MAX<span class="token punctuation">;</span> i <span class="token operator">+=</span> K<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		ans2 <span class="token operator">=</span> <span class="token punctuation">(</span>ans2 <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>SUM <span class="token operator">-</span><span class="token number">0ll</span><span class="token operator">-</span> i <span class="token operator">+</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> i <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> book<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> MOD <span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d %d %d\n"</span><span class="token punctuation">,</span> K<span class="token punctuation">,</span> ans1<span class="token punctuation">,</span> ans2 <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> inv2 <span class="token operator">%</span> MOD<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h3 id="B-紧急出口"><a href="#B-紧急出口" class="headerlink" title="B. 紧急出口"></a>B. 紧急出口</h3><div class="note info"><p>在一个走廊里共有 $N$ 个楼梯，其中 $N$ 个楼梯之间有 $N-1$ 个间隔， 个间隔会贴有一个紧急出口的指示牌指向左侧或者右侧。</p>
<p>问在所有的 $2N−1$ 种指示牌的贴法当中，有多少种贴法满足恰好有 $M$ 个楼梯(包括最左最右)的左右侧的紧急出口标识都不指向自己。</p>
<p>输出的答案对 $998244353$ 取模。<br>$N \le 10^3, M \le 10^9$</p>
</div>
<details class="note success"><summary><p>Solutiuon</p>
</summary>
<h4 id="倍增DP"><a href="#倍增DP" class="headerlink" title="倍增DP"></a>倍增DP</h4><div class="note danger"><p>待填</p>
</div>

<h4 id="隔板法"><a href="#隔板法" class="headerlink" title="隔板法"></a>隔板法</h4><p>称 “左右侧的紧急出口标识都不指向自己的点” 为关键点。</p>
<p>考虑如果选定了一个点是关键点，那么他们两边就形如 <code>&lt; o &gt;</code> ，可以发现相邻的两个关键点 $O_1, O_2$ 之间贴牌的方案一定形如 $…&lt; O_1 &gt; o &gt; … &gt; o &lt; … &lt; o &lt; O_2 &gt;…$ 显然，其实是把原序列分成连续的 $2m$ 段，每段内方向相同，相邻两段方向相反。 隔板即可。</p>
<p>考虑组合数的通项公式，项数只有 $m$ 项。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token function">C</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">2</span> <span class="token operator">*</span> m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</details>

<h3 id="C-合影"><a href="#C-合影" class="headerlink" title="C. 合影"></a>C. 合影</h3><div class="note info"><p>众所周知， ACM ICPC 是三人组队参赛的比赛。现在在 ICPC WF2020 现场，有 $k$ 支参赛队伍要站成一排拍照！ </p>
<p>摄影师小Z想促进队伍间的交流，于是小Z要求每个队伍的3个队员不能都挨在一起</p>
<p>现在小Z想问一问你，这 $k$ 支队伍有多少种拍照方式呢？由于数字很大，所以对 $998244353$ 取模就好~<br>$k \le 10^6$</p>
</div>
<details class="note success"><summary><p>Solution</p>
</summary>
<p>要求是每一队的三个队员不能全部挨在一起。 无法直接全部满足，考虑容斥。</p>
<p>假如所有人按任意顺序排列有 $(3k)!$ 种排列方式。</p>
<p>显然会算入了不合法的情况，考虑去除这些 钦定哪一队全部挨在一起了，如果强制某一队挨在一起可以吧那一队捆成一个人 这样的方案数为 $(3k - 2)!$，算入答案就是 $\dbinom{k}{1}(3k - 2)!(3!)^1$ 最后一个是因为同一队中的队员也有顺序。</p>
<p>显然会去除多了，如果有多队同时不合法，这些方案会被去除多次，钦定哪两队全部挨在一起了，算入答案就是 $\dbinom{k}{2}(3k - 4)!(3!)^2$。</p>
<p>显然加多了……</p>
<p>然后就是容斥原理了。</p>
<p>答案应该是 $\sum_{i=0}^{k}\limits{(-1)^i\dbinom{k}{i}(3k - 2i)!(3!)^i}$</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">C</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span> m<span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> frac<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> ifrac<span class="token punctuation">[</span>n <span class="token operator">-</span> m<span class="token punctuation">]</span> <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> ifrac<span class="token punctuation">[</span>m<span class="token punctuation">]</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> k<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">(</span>k <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> tans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		tans <span class="token operator">=</span> <span class="token function">C</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> frac<span class="token punctuation">[</span><span class="token number">3</span> <span class="token operator">*</span> k <span class="token operator">-</span> <span class="token number">2</span> <span class="token operator">*</span> i<span class="token punctuation">]</span> <span class="token operator">%</span> MOD <span class="token operator">*</span><span class="token number">1ll</span><span class="token operator">*</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> MOD<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
		tans <span class="token operator">=</span> <span class="token punctuation">(</span>i <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">?</span> MOD <span class="token operator">-</span> tans <span class="token operator">:</span> tans<span class="token punctuation">;</span>
		ans <span class="token operator">=</span> <span class="token punctuation">(</span>ans <span class="token operator">+</span><span class="token number">0ll</span><span class="token operator">+</span> tans<span class="token punctuation">)</span> <span class="token operator">%</span> MOD<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

</details>
<h3 id="D-路径"><a href="#D-路径" class="headerlink" title="D. 路径"></a>D. 路径</h3><div class="note info"><p>给定一个$n$个顶点的无根树，顶点编号 $1 ~ n$。</p>
<p>同时给定树上的 $k$ 条带权路径，要求支持以下操作:</p>
<ul>
<li><code>1 k</code> ，删除第 $k$ 条带权路径。 </li>
<li><code>2 k v</code> ，将第 $k$ 条带权路径的权值修改为 $v$ 。 </li>
<li><code>3 p</code> ，给定一个树上节点 $p$ ，询问所有没有被删去的带权路径中，不与该节点相交的所有路径中的最小权值。<br>$n, k, q \le 10^5$</li>
</ul>
</div>
<details class="note success"><summary><p>solution</p>
</summary>
<h4 id="做法一：树剖-std-set-维护标记-标记永久化线段树"><a href="#做法一：树剖-std-set-维护标记-标记永久化线段树" class="headerlink" title="做法一：树剖 +  std::set 维护标记 标记永久化线段树"></a>做法一：树剖 +  <code>std::set</code> 维护标记 标记永久化线段树</h4><p>$\mathcal{O}((k + q)\log_2^3{n})$<br>先树剖，路径补集仍然是线段树上 $\log_2(n)$ 段，可以在这些段里面加入可选的边，为了防止删除重复，<code>std::set</code> 内层数据类型为 <code>pair&lt;int, int&gt;</code> 分别存储路径的权值和编号。然后就直接做就好了。<br><del>后记：被出题人卡成了 $30$ ……。</del></p>
<details class="note warning"><summary><p>code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">,</span> __ <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> __<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> p<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> head<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edges</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> node<span class="token punctuation">;</span>
	<span class="token keyword">int</span> nxt<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>edge<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	tot<span class="token operator">++</span><span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>node <span class="token operator">=</span> v<span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt  <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span>        <span class="token operator">=</span> tot<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">struct</span> <span class="token class-name">Path</span><span class="token punctuation">&#123;</span> <span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> Val<span class="token punctuation">;</span> <span class="token function">Path</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> c<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> u <span class="token operator">=</span> a<span class="token punctuation">;</span> v <span class="token operator">=</span> b<span class="token punctuation">;</span> Val <span class="token operator">=</span> c<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span> <span class="token punctuation">&#125;</span> P<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> dfn<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> rnk<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> dfc<span class="token punctuation">,</span> dep<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> top<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> fa<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> si<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> son<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token keyword">int</span> now<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">,</span> <span class="token keyword">int</span> dp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	fa<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> f<span class="token punctuation">;</span>  dep<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">;</span> <span class="token keyword">int</span> <span class="token operator">&amp;</span>Mid <span class="token operator">=</span> son<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span> <span class="token operator">&amp;</span>Si <span class="token operator">=</span> si<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token operator">==</span> f<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token function">dfs0</span><span class="token punctuation">(</span>ex<span class="token punctuation">,</span> now<span class="token punctuation">,</span> dp <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Si <span class="token operator">+=</span> si<span class="token punctuation">[</span>ex<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>si<span class="token punctuation">[</span>ex<span class="token punctuation">]</span> <span class="token operator">></span> si<span class="token punctuation">[</span>Mid<span class="token punctuation">]</span><span class="token punctuation">)</span> Mid <span class="token operator">=</span> ex<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token keyword">int</span> now<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">,</span> <span class="token keyword">int</span> tp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	dfn<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token operator">++</span>dfc<span class="token punctuation">;</span>  top<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> tp<span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>son<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">dfs1</span><span class="token punctuation">(</span>son<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">,</span> now<span class="token punctuation">,</span> tp<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token operator">==</span> f <span class="token operator">||</span> ex <span class="token operator">==</span> son<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token function">dfs1</span><span class="token punctuation">(</span>ex<span class="token punctuation">,</span> now<span class="token punctuation">,</span> ex<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">namespace</span> SegmentTree<span class="token punctuation">&#123;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">3e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ch<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> set<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span> <span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span> less <span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span> <span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">></span> <span class="token operator">></span> v<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">make</span> <span class="token punctuation">(</span>tot<span class="token operator">++</span><span class="token punctuation">,</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> v<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tot<span class="token punctuation">)</span></span></span>
	<span class="token keyword">int</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token keyword">return</span> make<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>INT_MAX<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span> <span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>INT_MAX<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">build</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> <span class="token keyword">int</span> V<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">></span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">update_del</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">,</span> <span class="token keyword">int</span> V<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">></span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> nowl <span class="token operator">&amp;&amp;</span> nowr <span class="token operator">&lt;=</span> R<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update_del</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>R  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update_del</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> V<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">,</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> ans <span class="token operator">=</span> <span class="token function">make_pair</span><span class="token punctuation">(</span>INT_MAX<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>nowl <span class="token operator">==</span> nowr<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> ans <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> p <span class="token operator">&lt;=</span> mid <span class="token operator">?</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> p<span class="token punctuation">,</span> ans<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> p<span class="token punctuation">,</span> ans<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>query<span class="token punctuation">;</span> 	<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update_add<span class="token punctuation">;</span> <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>Groot<span class="token punctuation">;</span> 
  <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>build<span class="token punctuation">;</span>		<span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update_del<span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">fi first</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">se second</span></span>
vector<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> <span class="token operator">></span> TMP<span class="token punctuation">;</span>
<span class="token keyword">int</span> root <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">AddWithoutPath</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> Val<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	TMP<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">!=</span> top<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>top<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		TMP<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dfn<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> u <span class="token operator">=</span> fa<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
	TMP<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">,</span> dfn<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>TMP<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> TMP<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> TMP<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">update_add</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">update_add</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> TMP<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">update_add</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>TMP<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>se <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">DelWithoutPath</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> Val<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	TMP<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">!=</span> top<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>top<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		TMP<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dfn<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> u <span class="token operator">=</span> fa<span class="token punctuation">[</span>top<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
	TMP<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">,</span> dfn<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>TMP<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> TMP<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> TMP<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">update_del</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>se <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">update_del</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> TMP<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>fi <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">update_del</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> TMP<span class="token punctuation">[</span>TMP<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>se <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> Val<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> <span class="token comment">// freopen("in.txt", "r", stdin);</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> u<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> p<span class="token punctuation">,</span> q<span class="token punctuation">,</span> v<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		P<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">Path</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span> q<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	root <span class="token operator">=</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span> <span class="token function">AddWithoutPath</span><span class="token punctuation">(</span>P<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> P<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> P<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>Val<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>p<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> opt<span class="token punctuation">,</span> k<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>opt <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token function">DelWithoutPath</span><span class="token punctuation">(</span>P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>Val<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>opt <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token function">DelWithoutPath</span><span class="token punctuation">(</span>P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>Val<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">Read</span><span class="token punctuation">(</span>P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>Val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">AddWithoutPath</span><span class="token punctuation">(</span>P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> P<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>Val<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
			pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span> r <span class="token operator">=</span> <span class="token function">query</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> dfn<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> r<span class="token punctuation">.</span>first <span class="token operator">&lt;</span> INT_MAX <span class="token operator">?</span> r<span class="token punctuation">.</span>first <span class="token operator">:</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>

<h4 id="做法二：线段树维护路径交集-二分最小值"><a href="#做法二：线段树维护路径交集-二分最小值" class="headerlink" title="做法二：线段树维护路径交集 + 二分最小值"></a>做法二：线段树维护路径交集 + 二分最小值</h4><blockquote>
<p>巧妙绝伦的做法。</p>
</blockquote>
<p>要求回答的是：不与节点 $p$ 相交的所有路径中的最小权值。</p>
<p>考虑二分一个最小权值 $x$，然后判断是所有权值小于 $x$ 的路径是不是全都和点 $p$ 有交。</p>
<p>如果把所有路径按照权值排序，那么成为答案的那条路径之前的路径一定都和点 $p$ 有交，这就可以直接二分答案了。</p>
<p>但是路径需要支持删除和修改。 显然需要数据结构。</p>
<p>一个显然的事实是：两条路径的交 也是一条路径（定义一个点或者空都属于路径）。 </p>
<p>线段树就可以快速求出一段路径区间内所有路径的交集路径。</p>
<p>所有路径按照权值排好序后的一个前缀全部和点 $p$ 有交，等价于，这个前缀所有的路径的交集和点 $p$ 有交。</p>
<p>对于删除操作，可以考虑在线段树对应操作上打删除标记。对于修改操作，因为没有强制在线，可以考虑一开始把这条路径的所有权值排好序后加入线段树，然后对于当前无用的版本，打上删除标记即可。对于一次修改，转化成一次删除和一次恢复操作。</p>
<p>当然平衡树可以轻易完成这样的工作，在平衡树上维护树上路径交集，写不出来啊…</p>
<p>这样就可以直接用线段树求出某个前缀的所有路径的交集路径了。线段树合并信息时，需要询问 <code>LCA</code>，算上二分，这样的单次询问复杂度可以做到 $\mathcal{O}(\log k \log w \log n)$（倍增 <code>LCA</code>），或者 $\mathcal{O}(\log k \log w)$（$\mathcal{O}(n) - \mathcal{O}(1)$ <code>LCA</code>）。</p>
<p>注意到线段树本身就是一个分治的结构，可以直接在线段树上二分，这样就可以做到 $\mathcal{O}(\log k \log n)$（倍增 <code>LCA</code>），或者 $\mathcal{O}(\log k)$（$\mathcal{O}(n) - \mathcal{O}(1)$ <code>LCA</code>）。</p>
<p>我的实现是 倍增 <code>LCA</code> + 线段树二分，算上预处理，总时间复杂度为 $\mathcal{O}(n\log(n) + q\log(q+k)\log n)$</p>
<details class="note warning"><summary><p>code</p>
</summary>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">2e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> head<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">,</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edges</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> node<span class="token punctuation">;</span>
	<span class="token keyword">int</span> nxt<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>edge<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	tot<span class="token operator">++</span><span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>node <span class="token operator">=</span> v<span class="token punctuation">;</span>
	edge<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt  <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span>        <span class="token operator">=</span> tot<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> q<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> LOG <span class="token operator">=</span> <span class="token number">18</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> fa<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span>LOG <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dep<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token keyword">int</span> now<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">,</span> <span class="token keyword">int</span> dp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	fa<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> f<span class="token punctuation">;</span> dep<span class="token punctuation">[</span>now<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nxt<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ex <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ex <span class="token operator">==</span> f<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token function">dfs0</span><span class="token punctuation">(</span>ex<span class="token punctuation">,</span> now<span class="token punctuation">,</span> dp <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
map <span class="token operator">&lt;</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> <span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">></span>  Mem<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">LCA</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> P <span class="token operator">=</span> <span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token function">min</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">max</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>Mem<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>P<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> Mem<span class="token punctuation">[</span>P<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> d <span class="token operator">=</span> dep<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">-</span> dep<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> x <span class="token operator">=</span> u<span class="token punctuation">,</span> y <span class="token operator">=</span> v<span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> LOG<span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>d <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span> x <span class="token operator">=</span> fa<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">==</span> y<span class="token punctuation">)</span> <span class="token keyword">return</span> x<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> LOG<span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>fa<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> fa<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		x <span class="token operator">=</span> fa<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> y <span class="token operator">=</span> fa<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> Mem<span class="token punctuation">[</span>P<span class="token punctuation">]</span> <span class="token operator">=</span> fa<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">void</span> <span class="token function">InitQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">dfs0</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> LOG<span class="token punctuation">)</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> fa<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> fa<span class="token punctuation">[</span>fa<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">struct</span> <span class="token class-name">Path_t</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> id<span class="token punctuation">,</span> u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">,</span> ver<span class="token punctuation">;</span>
	<span class="token function">Path_t</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> c<span class="token punctuation">,</span> <span class="token keyword">int</span> d<span class="token punctuation">,</span> <span class="token keyword">int</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> id <span class="token operator">=</span> a<span class="token punctuation">;</span> u <span class="token operator">=</span> b<span class="token punctuation">;</span> v <span class="token operator">=</span> c<span class="token punctuation">;</span> w <span class="token operator">=</span> d<span class="token punctuation">;</span> ver <span class="token operator">=</span> e<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token function">Path_t</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">CMP</span><span class="token punctuation">(</span><span class="token keyword">const</span> Path_t <span class="token operator">&amp;</span> A<span class="token punctuation">,</span> <span class="token keyword">const</span> Path_t <span class="token operator">&amp;</span> B<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> A<span class="token punctuation">.</span>w <span class="token operator">&lt;</span> B<span class="token punctuation">.</span>w<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">struct</span> <span class="token class-name">Q_t</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> type<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">;</span>
	<span class="token function">Q_t</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">int</span> z<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> type <span class="token operator">=</span> x<span class="token punctuation">;</span> a <span class="token operator">=</span> y<span class="token punctuation">;</span> b <span class="token operator">=</span> z<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token function">Q_t</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>Q<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> nowv<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span>Path_t<span class="token operator">></span> P<span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Pv<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">dis</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> dep<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">+</span> dep<span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token punctuation">(</span>dep<span class="token punctuation">[</span><span class="token function">LCA</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> <span class="token function">cross</span><span class="token punctuation">(</span><span class="token keyword">int</span> u0<span class="token punctuation">,</span> <span class="token keyword">int</span> v0<span class="token punctuation">,</span> <span class="token keyword">int</span> u1<span class="token punctuation">,</span> <span class="token keyword">int</span> v1<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> t0 <span class="token operator">=</span> <span class="token function">LCA</span><span class="token punctuation">(</span>u0<span class="token punctuation">,</span> v0<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> t1 <span class="token operator">=</span> <span class="token function">LCA</span><span class="token punctuation">(</span>u0<span class="token punctuation">,</span> u1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> t2 <span class="token operator">=</span> <span class="token function">LCA</span><span class="token punctuation">(</span>u0<span class="token punctuation">,</span> v1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> t3 <span class="token operator">=</span> <span class="token function">LCA</span><span class="token punctuation">(</span>v0<span class="token punctuation">,</span> u1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> t4 <span class="token operator">=</span> <span class="token function">LCA</span><span class="token punctuation">(</span>v0<span class="token punctuation">,</span> v1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> target0 <span class="token operator">=</span> t0<span class="token punctuation">;</span> <span class="token comment">// t0 t1 t3 </span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>target0<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>t1<span class="token punctuation">]</span><span class="token punctuation">)</span> target0 <span class="token operator">=</span> t1<span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>target0<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>t3<span class="token punctuation">]</span><span class="token punctuation">)</span> target0 <span class="token operator">=</span> t3<span class="token punctuation">;</span>
	<span class="token keyword">int</span> target1 <span class="token operator">=</span> t0<span class="token punctuation">;</span> <span class="token comment">// t0 t2 t4</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>target1<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>t2<span class="token punctuation">]</span><span class="token punctuation">)</span> target1 <span class="token operator">=</span> t2<span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token punctuation">[</span>target1<span class="token punctuation">]</span> <span class="token operator">&lt;</span> dep<span class="token punctuation">[</span>t4<span class="token punctuation">]</span><span class="token punctuation">)</span> target1 <span class="token operator">=</span> t4<span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>target1 <span class="token operator">==</span> target0 <span class="token operator">&amp;&amp;</span> <span class="token function">dis</span><span class="token punctuation">(</span>target1<span class="token punctuation">,</span> u1<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">dis</span><span class="token punctuation">(</span>target1<span class="token punctuation">,</span> v1<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token function">dis</span><span class="token punctuation">(</span>u1<span class="token punctuation">,</span> v1<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">make_pair</span><span class="token punctuation">(</span>target0<span class="token punctuation">,</span> target1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">bool</span> <span class="token function">crossx</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>u <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">||</span> v <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>  <span class="token keyword">return</span> <span class="token function">dis</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token function">dis</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> x<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">dis</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
<span class="token keyword">namespace</span> SegmentTree<span class="token punctuation">&#123;</span> <span class="token comment">// To maintain the merge path</span>
	<span class="token keyword">struct</span> <span class="token class-name">Node_t</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span>
		<span class="token keyword">bool</span> isDel<span class="token punctuation">,</span> isNul<span class="token punctuation">;</span>
		<span class="token keyword">void</span> <span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			isDel <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> isNul <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> u <span class="token operator">=</span> v <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token keyword">const</span> <span class="token keyword">int</span> _ <span class="token operator">=</span> <span class="token number">1e6</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ch<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tot <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> Node_t v<span class="token punctuation">[</span>_<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">(</span>ch<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression"><span class="token function">make</span> <span class="token punctuation">(</span>tot<span class="token operator">++</span><span class="token punctuation">,</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> ch<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> v<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tot<span class="token punctuation">)</span></span></span>
	<span class="token keyword">int</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">return</span> make<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">maintain</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">=</span> <span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">&amp;&amp;</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel<span class="token punctuation">)</span><span class="token punctuation">;</span>
		v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">=</span> <span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">||</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isNul<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isNul<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel<span class="token punctuation">)</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>u <span class="token operator">=</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>v <span class="token operator">=</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel<span class="token punctuation">)</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>u <span class="token operator">=</span> v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>v <span class="token operator">=</span> v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
				pair<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> R <span class="token operator">=</span> <span class="token function">cross</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>first <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
					v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span> 
				v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>u <span class="token operator">=</span> R<span class="token punctuation">.</span>first<span class="token punctuation">;</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>v <span class="token operator">=</span> R<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> L<span class="token punctuation">,</span> <span class="token keyword">int</span> R<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>L <span class="token operator">==</span> R<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">=</span> <span class="token punctuation">(</span>P<span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>ver <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>u     <span class="token operator">=</span> P<span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">;</span>
			v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>v     <span class="token operator">=</span> P<span class="token punctuation">[</span>L <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">return</span> <span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span> <span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">=</span> make<span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">build</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> L<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> R<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>  
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">,</span> <span class="token keyword">bool</span> V<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>nowl <span class="token operator">==</span> nowr<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">=</span> V<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>p <span class="token operator">&lt;=</span> mid<span class="token punctuation">)</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> p<span class="token punctuation">,</span> V<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>p  <span class="token operator">></span> mid<span class="token punctuation">)</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> p<span class="token punctuation">,</span> V<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">maintain</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> o<span class="token punctuation">,</span> <span class="token keyword">int</span> nowl<span class="token punctuation">,</span> <span class="token keyword">int</span> nowr<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>isDel <span class="token operator">&amp;&amp;</span> <span class="token function">crossx</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span>o<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>nowl <span class="token operator">==</span> nowr<span class="token punctuation">)</span> <span class="token keyword">return</span> P<span class="token punctuation">[</span>nowl <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
		<span class="token keyword">bool</span> r0 <span class="token operator">=</span> <span class="token operator">!</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">||</span> <span class="token operator">!</span><span class="token function">crossx</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">||</span> v<span class="token punctuation">[</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel<span class="token punctuation">;</span>
		<span class="token keyword">bool</span> r1 <span class="token operator">=</span> <span class="token operator">!</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isNul <span class="token operator">||</span> <span class="token operator">!</span><span class="token function">crossx</span><span class="token punctuation">(</span>v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">||</span> v<span class="token punctuation">[</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>isDel<span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>nowl <span class="token operator">+</span> nowr<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>r0<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">ls</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> nowl<span class="token punctuation">,</span> mid<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>r1<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token function">rs</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> nowr<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>  <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>build<span class="token punctuation">;</span> <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>Groot<span class="token punctuation">;</span>
   <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>query<span class="token punctuation">;</span> <span class="token keyword">using</span> SegmentTree<span class="token operator">::</span>update<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token function">freopen</span><span class="token punctuation">(</span><span class="token string">"in.txt"</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">Read</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> u<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token function">InitQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span>
		P<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Path_t</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> u<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> nowv<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> type<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">;</span> <span class="token function">Read</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">Read</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">,</span> Q<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">Q_t</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> Q<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">Q_t</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Q_t <span class="token operator">&amp;</span>now <span class="token operator">=</span> Q<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span>type <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> P<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Path_t</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span>a<span class="token punctuation">,</span> P<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span> P<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span> now<span class="token punctuation">.</span>b<span class="token punctuation">,</span> <span class="token operator">++</span>nowv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span> Pv<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>nowv<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>P<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> P<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> CMP<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> P<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> Pv<span class="token punctuation">[</span>P<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id<span class="token punctuation">]</span><span class="token punctuation">[</span>P<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>ver<span class="token punctuation">]</span> <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> root <span class="token operator">=</span> <span class="token function">Groot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> U<span class="token punctuation">;</span> U <span class="token operator">=</span> P<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">build</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> U<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> k<span class="token punctuation">)</span> nowv<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">rep</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Q_t <span class="token operator">&amp;</span>now <span class="token operator">=</span> Q<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span>type <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> Pl <span class="token operator">=</span> Pv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span>nowv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> U<span class="token punctuation">,</span> Pl<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span>type <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> Pl0 <span class="token operator">=</span> Pv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span>  nowv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> Pl1 <span class="token operator">=</span> Pv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">++</span>nowv<span class="token punctuation">[</span>now<span class="token punctuation">.</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> U<span class="token punctuation">,</span> Pl0<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">update</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> U<span class="token punctuation">,</span> Pl1<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span> 
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> <span class="token function">query</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> U<span class="token punctuation">,</span> now<span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</details>
<h4 id="做法三：CDQ-分治"><a href="#做法三：CDQ-分治" class="headerlink" title="做法三：CDQ 分治"></a>做法三：CDQ 分治</h4><div class="note warning"><p>学了再说。</p>
</div>


</details>

<p>QAQ  /kel</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a target="_blank" href="//tags/学习总结/" rel="tag noopener"># 学习总结</a>
              <a target="_blank" href="//tags/好题/" rel="tag noopener"># 好题</a>
              <a target="_blank" href="//tags/题解/" rel="tag noopener"># 题解</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/「琐记」NOIP-2020-考前/" rel="prev" title="「琐记」NOIP 2020 考前">
                  <i class="fa fa-chevron-left"></i> 「琐记」NOIP 2020 考前
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/「学习总结」数据结构/" rel="next" title="「学习总结」数据结构">
                  「学习总结」数据结构 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
  
  
  



      
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">舒雨墨</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">319k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:50</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/@next-theme/pjax@0.4.0/pjax.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>
  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '.page-configurations',
    '.main-inner',
    '.post-toc-wrap',
    '.languages',
    '.pjax'
  ],
  analytics: false,
  cacheBust: false,
  scrollRestoration: false,
  scrollTo: !CONFIG.bookmark.enable
});

document.addEventListener('pjax:success', () => {
  pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  const hasTOC = document.querySelector('.post-toc');
  document.querySelector('.sidebar-inner').classList.toggle('sidebar-nav-active', hasTOC);
  document.querySelector(hasTOC ? '.sidebar-nav-toc' : '.sidebar-nav-overview').click();
  NexT.utils.updateSidebarPosition();
});
</script>


  
  <script data-pjax>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  <script src="/js/local-search.js"></script>












  






<script data-pjax>
  (function() {
    function leancloudSelector(url) {
      return document.getElementById(url).querySelector('.leancloud-visitors-count');
    }

    function addCount(Counter) {
      const visitors = document.querySelector('.leancloud_visitors');
      const url = decodeURI(visitors.id);
      const title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            const counter = results[0];
            leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .catch(error => {
                console.error('Failed to save visitor count', error);
              });
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    function showTime(Counter) {
      const visitors = document.querySelectorAll('.leancloud_visitors');
      const entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });
      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            const target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    const { app_id, app_key, server_url } = {"enable":true,"app_id":"Tw3wqSuCQwL4zK5pGCd6BkKa-gzGzoHsz","app_key":"lwlhdaYvNPPDLhBEGeUegbH6","server_url":null,"security":false};
    function fetchData(api_server) {
      const Counter = (method, url, data) => {
        return fetch(`${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : app_id,
            'X-LC-Key'    : app_key,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (location.hostname == '127.0.0.1' || location.hostname == 'localhost') ;
        else addCount(Counter);
      }
      if (document.querySelectorAll('.post-title-link').length >= 1 || document.querySelectorAll('.post-title').length >= 1) {
        showTime(Counter);
      }
    }

    const api_server = app_id.slice(-9) !== '-MdYXbMMI' ? server_url : `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;

    if (api_server) {
      fetchData(api_server);
    } else {
      fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
        .then(response => response.json())
        .then(({ api_server }) => {
          fetchData('https://' + api_server);
        });
    }
  })();
</script>


    <div class="pjax">
  

  
      <script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              const target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    const script = document.createElement('script');
    script.src = '//cdn.jsdelivr.net/npm/mathjax@3.1.0/es5/tex-mml-chtml.js';
    script.defer = true;
    document.head.appendChild(script);
  } else {
    MathJax.startup.document.state(0);
    MathJax.typesetClear();
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  
<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: "2020/「学习总结」清北学堂-十连测/",
    }, {"enable":true,"appId":"Tw3wqSuCQwL4zK5pGCd6BkKa-gzGzoHsz","appKey":"lwlhdaYvNPPDLhBEGeUegbH6","placeholder":"Just go go","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-cn","visitor":false,"comment_count":true,"recordIP":true,"serverURLs":null,"enableQQ":true,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

    </div>
</body>
</html>
